Last active
June 8, 2017 01:19
-
-
Save kmizu/a135d96cb7a68fdcf9e43fa1b18ac3a7 to your computer and use it in GitHub Desktop.
Scalaは(便利な)オブジェクト指向言語です ref: http://qiita.com/kmizu/items/cfaabdbfe3d3b4ef0640
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Point(val x: Int, val y: Int) | |
val p = new Point(1, 2) | |
println(p.x) // => 1 | |
println(p.y) // => 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Point(val x: Int, val y: Int) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class Person(name: String, age: Int) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val x: Int => Int = {x => x} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val x: Function1[Int, Int] = new Function1[Int, Int] { | |
def apply(x: Int): Int = x | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Hoge | |
Foo | |
Bar | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
implicit class RichString(self: String) { | |
def show(): Unit = println(self) | |
} | |
"Hello, World!".show() // => "Hello, World!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val p = new Point(1, 2) | |
p.x _ // => () => Int | |
p.y _ // => () => Int |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def add(x: Int, y: Int): Int = { | |
def a(x: Int, y: Int): Int = x + y | |
a(x, y) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def f(arg1: Type1 ...): ReturnType = Expression |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def f(x: Int, y: Int): Int = { | |
x + y | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def f(x: Int, y: Int): Int = x + y |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> buffer.toList | |
res4: List[Int] = List(1, 2, 4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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