作为scala工厂的伴侣对象

时间:2013-06-02 19:21:27

标签: scala factory point companion-object

我刚开始使用Scala,并正在开发一些教程。我遇到了伴侣对象,并将它们用作工厂。我尝试了几件事。但是,我没有得到以下工作正常。不能理解它..

import math._

abstract class Point{
  // ...
}
object Point{
  private class PointInt(val x:Int,val y:Int) extends Point{
    def +(that:PointInt) = new PointInt(this.x + that.x, this.y + that.y)
    def distance(that:PointInt) = 
      sqrt(pow((this.x - that.x), 2) + pow((this.y - that.y), 2))
  }
  private class PointDouble(val x:Double,val y:Double) extends Point{
    def +(that:PointDouble) = new PointDouble(this.x + that.x, this.y + that.y)
    def distance(that:PointDouble) = 
      sqrt(pow((this.x - that.x), 2) + pow((this.y - that.y), 2))
  }
  def apply(x:Int,y:Int):Point = new PointInt(x,y)
  def apply(x:Double,y:Double):Point = new PointDouble(x,y)
}

val a = Point(1,2)
val b = Point(3,4)
val c = a+b // does not work... 

只是试图加起来两个整数点,就像我在方法中定义的那样......有谁知道我做错了什么?

编辑:我当时试图在工厂中包装以下(工作)类。

class Point(val x:Int,val y:Int){
  def +(that:Point) = new Point(this.x + that.x, this.y + that.y)
  def distance(that:Point) = sqrt(pow((this.x - that.x),2) + pow((this.y - that.y),2))

}

val a = new Point(1,2)              //> a  : week1.OU2.Point = week1.OU2$Point@73e48fa7
val b = new Point(3,4)              //> b  : week1.OU2.Point = week1.OU2$Point@677bb8fe
val c = a+b                         //> c  : week1.OU2.Point = week1.OU2$Point@6bae60c5
c.x                                 //> res0: Int = 4
c.y                                 //> res1: Int = 6

1 个答案:

答案 0 :(得分:1)

我不太确定实际上对您施加了哪些约束,例如,哪些类应该/必须是私有的,但使用F-bounded多态可能是您所需解决方案的跳板。

/* Simplified interface (adding sqrt is straight-forward) */

abstract class Point[P <: Point[P]] {
  def +(that: P): P
}

/* Two implementations */

class PointInt(val x:Int,val y:Int) extends Point[PointInt] {
  def +(that:PointInt) = new PointInt(this.x + that.x, this.y + that.y)
}

class PointDouble(val x:Double,val y:Double) extends Point[PointDouble] {
  def +(that:PointDouble) = new PointDouble(this.x + that.x, this.y + that.y)
}

/* Companion object */

object Point {
  def apply(x:Int,y:Int) = new PointInt(x,y)
  def apply(x:Double,y:Double) = new PointDouble(x,y)
}

/* Use cases */

val a = Point(1,2)
val b = Point(3,4)
val c = a+b // ok
val d = Point(1.0, 2.5)
val e = c+d // error: type mismatch

但是,请注意,如果您想要隐藏您的实现,这将无法帮助您,即将它们设为私有并仅使用通用Point声明公共接口 - 正如其他人已经指出的那样。

相关问题