Skip to content

Instantly share code, notes, and snippets.

@kmizu
Last active June 8, 2017 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmizu/a135d96cb7a68fdcf9e43fa1b18ac3a7 to your computer and use it in GitHub Desktop.
Save kmizu/a135d96cb7a68fdcf9e43fa1b18ac3a7 to your computer and use it in GitHub Desktop.
Scalaは(便利な)オブジェクト指向言語です ref: http://qiita.com/kmizu/items/cfaabdbfe3d3b4ef0640
class Point(val x: Int, val y: Int)
val p = new Point(1, 2)
println(p.x) // => 1
println(p.y) // => 2
class Point(val x: Int, val y: Int)
case class Person(name: String, age: Int)
val x: Int => Int = {x => x}
val x: Function1[Int, Int] = new Function1[Int, Int] {
def apply(x: Int): Int = x
}
"""
Hoge
Foo
Bar
"""
implicit class RichString(self: String) {
def show(): Unit = println(self)
}
"Hello, World!".show() // => "Hello, World!"
val p = new Point(1, 2)
p.x _ // => () => Int
p.y _ // => () => Int
def add(x: Int, y: Int): Int = {
def a(x: Int, y: Int): Int = x + y
a(x, y)
}
def f(arg1: Type1 ...): ReturnType = Expression
def f(x: Int, y: Int): Int = {
x + y
}
def f(x: Int, y: Int): Int = x + y
scala> import scala.collection.mutable.Buffer
import scala.collection.mutable.Buffer
scala> val buffer = Buffer[Int]()
buffer: scala.collection.mutable.Buffer[Int] = ArrayBuffer()
scala> buffer += 1
res1: buffer.type = ArrayBuffer(1)
scala> buffer += 2
res2: buffer.type = ArrayBuffer(1, 2)
scala> buffer += 4
res3: buffer.type = ArrayBuffer(1, 2, 4)
scala> buffer.toList
res4: List[Int] = List(1, 2, 4)
class Person {
private final String name;
private final int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public int hashCode() { .... }
public boolean equals(Object other) { ... }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment