“A Taste of Concurrency”

shapes.scala:

package com.ch1

// code-examples/IntroducingScala/shapes.scala
package shapes {
  class Point(val x: Double, val y: Double) {
    override def toString() = "Point(" + x + "," + y + ")"
    //重载toString方法
  }
  abstract class Shape() {
    def draw(): Unit
  }
  //val----->强类型
  class Circle(val center: Point, val radius: Double) extends Shape {
    def draw() = println("Circle.draw: " + this)
    override def toString() = "Circle(" + center + "," + radius + ")"
  }
  class Rectangle(val lowerLeft: Point, val height: Double, val width: Double)
    extends Shape {
    def draw() = println("Rectangle.draw: " + this)
    override def toString() =
      "Rectangle(" + lowerLeft + "," + height + "," + width + ")"
  }
  class Triangle(val point1: Point, val point2: Point, val point3: Point)
    extends Shape {
    def draw() = println("Triangle.draw: " + this)
    override def toString() =
      "Triangle(" + point1 + "," + point2 + "," + point3 + ")"
  }
}package com.ch1

// code-examples/IntroducingScala/shapes.scala
package shapes {
  class Point(val x: Double, val y: Double) {
    override def toString() = "Point(" + x + "," + y + ")"
    //重载toString方法
  }
  abstract class Shape() {
    def draw(): Unit
  }
  //val----->强类型
  class Circle(val center: Point, val radius: Double) extends Shape {
    def draw() = println("Circle.draw: " + this)
    override def toString() = "Circle(" + center + "," + radius + ")"
  }
  class Rectangle(val lowerLeft: Point, val height: Double, val width: Double)
    extends Shape {
    def draw() = println("Rectangle.draw: " + this)
    override def toString() =
      "Rectangle(" + lowerLeft + "," + height + "," + width + ")"
  }
  class Triangle(val point1: Point, val point2: Point, val point3: Point)
    extends Shape {
    def draw() = println("Triangle.draw: " + this)
    override def toString() =
      "Triangle(" + point1 + "," + point2 + "," + point3 + ")"
  }
}


shapesActor.scala:
package com.ch1

package shapes {
  import scala.actors.Actor._
import scala.actors._
import com.ch1.shapes._
  object ShapeDrawingActor extends Actor {
    def act() {
      loop {
        receive {
          case s: Shape => s.draw()
          case "exit"   => println("exiting..."); exit
          case x: Any   => println("Error: Unknown message! " + x)
        }
      }
    }
  }
}

shapesScript.scala:
package com.ch1

object shapesScript {
  def main(args: Array[String]): Unit = {

    import shapes._

    ShapeDrawingActor.start()

    ShapeDrawingActor ! new Circle(new Point(0.0, 0.0), 1.0)
    ShapeDrawingActor ! new Rectangle(new Point(0.0, 0.0), 2, 5)
    ShapeDrawingActor ! new Triangle(new Point(0.0, 0.0),
      new Point(1.0, 0.0),
      new Point(0.0, 1.0))
    ShapeDrawingActor ! 3.14159

    ShapeDrawingActor ! "exit"

  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值