package com.hengheng.scala class Point(val x : Int, val y : Int) { val isOriginal : Boolean = { x == 0 && y == 0 } } class TalkPoint(x : Int, y : Int) extends Point(x, y) { def talk() = { println("My position is (" + x + "," + y + ")") } } class HappyTalkPoint(x : Int, y : Int) extends TalkPoint(x, y) { override def talk() { println("I'am happy!") super.talk() } } abstract class Animal { def walk(speed : Int) def breathe() = { println("Aninamal breathes.") } } class Dog extends Animal { override def walk(speed : Int) = { println("Dog walks, speed is " + speed) } } object Application{ def main(args : Array[String]) { val p = new Point(0, 0) println("point info(" + p.x + "," + p.y + ")") println(p.isOriginal) val pp = new TalkPoint(3,4) pp.talk() val ppp = new HappyTalkPoint(0, 0) ppp.talk() val d = new Dog() d.walk(10) d.breathe } }
输出:
point info(0,0)
true
My position is (3,4)
I'am happy!
My position is (0,0)
Dog walks, speed is 10
Aninamal breathes.