Created
July 8, 2013 02:37
-
-
Save f81/5945879 to your computer and use it in GitHub Desktop.
第5章:型について語る ref: http://qiita.com/f81@github/items/f98e157210c0b83e5842
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
public class Car { | |
public static void main(String[] args) { | |
final Car car = new Car("blue"); | |
car.run(); | |
} | |
private static void output(String message) { | |
System.out.println(message); | |
} | |
private final String color; | |
public Car(String color) { | |
this.color = color; | |
} | |
public String getColor() { | |
return color; | |
} | |
public void run() { | |
output("Start!!!"); | |
} | |
} |
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
object Car { | |
def main(args: Array[String]){ | |
var car = new Car("blue") | |
car.run | |
} | |
private def output(message: String):Unit=println(message) | |
} | |
class Car(color: String) { | |
def run = { | |
Car.output("Start!!!") | |
} | |
} |
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
object CarExtends { | |
def main(args: Array[String]){ | |
var car = new Car("blue") | |
output(car) | |
car.run | |
var truck = new Truck("Silver") | |
output(truck) | |
truck.run | |
} | |
def output(car: Car):Unit=println("Color is " + car.color + ".") | |
} | |
class Car(val color: String) { | |
def run = println("Start!!!") | |
} | |
class Truck(color: String) extends Car(color) { | |
override def run =println("Truck start!!!") | |
} |
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 | |
Welcome to Scala version 2.10.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_25). | |
Type in expressions to have them evaluated. | |
Type :help for more information. | |
scala> |
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> val i = 1 | |
i: Int = 1 |
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 output(car: Car):Unit=println("Color is " + car.color + ".") |
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
var car = new Car("blue") | |
output(car) |
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
var truck = new Truck("Silver") | |
output(truck) |
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> val d = 1.5 | |
d: Double = 1.5 |
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> val s = "雷電" | |
s: String = "雷電" |
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
$ scalac Car.scala | |
$ scala Car | |
Start!!! |
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 run = { | |
Car.output("Start!!!") | |
} |
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
$ scalac CarExtends.scala | |
$ scala CarExtends | |
Color is blue. | |
Start!!! | |
Color is Silver. | |
Truck start!!! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment