Skip to content

Instantly share code, notes, and snippets.

@f81
Created July 8, 2013 02:37
Show Gist options
  • Save f81/5945879 to your computer and use it in GitHub Desktop.
Save f81/5945879 to your computer and use it in GitHub Desktop.
第5章:型について語る ref: http://qiita.com/f81@github/items/f98e157210c0b83e5842
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!!!");
}
}
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!!!")
}
}
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!!!")
}
$ 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>
scala> val i = 1
i: Int = 1
def output(car: Car):Unit=println("Color is " + car.color + ".")
var car = new Car("blue")
output(car)
var truck = new Truck("Silver")
output(truck)
scala> val d = 1.5
d: Double = 1.5
scala> val s = "雷電"
s: String = "雷電"
$ scalac Car.scala
$ scala Car
Start!!!
def run = {
Car.output("Start!!!")
}
$ 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