码迷,mamicode.com
首页 > 其他好文 > 详细

SCALA常规练习B

时间:2015-04-26 13:40:21      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:

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.

SCALA常规练习B

标签:

原文地址:http://www.cnblogs.com/aguncn/p/4457603.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!