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"
}
}