在Scala中使用抽象类和工厂时相互调用方法(伴随对象)

Posted

技术标签:

【中文标题】在Scala中使用抽象类和工厂时相互调用方法(伴随对象)【英文标题】:Calling methods from each other when using Abstact Class and Factory in Scala (Companion Object) 【发布时间】:2014-05-29 03:36:30 【问题描述】:

我正在关注“Scala 中的编程”,第 10 章类型层次结构 (http://www.artima.com/pins1ed/composition-and-inheritance.html)。

我在 Scala 交互式 shell 中收到以下错误:

scala> :load Element.scala
Loading Element.scala...
<console>:11: error: not found: type Element
                   ) extends Element
                             ^
<console>:13: error: not found: type Element
           private class LineElement(s: String) extends Element 
                                                        ^
<console>:23: error: not found: type Element
                   ) extends Element 
                             ^
<console>:28: error: not found: type Element
           def elem(contents:  Array[String]): Element = new ArrayElement(contents)
                                               ^
<console>:30: error: not found: type Element
           def elem(chr: Char, width: Int, height: Int): Element = new UniformElement(chr, width, height)
                                                         ^
<console>:32: error: not found: type Element
           def elem(line: String): Element = new LineElement(line)
                                   ^
<console>:7: error: not found: value Element
       import Element.elem
              ^
<console>:16: error: not found: value elem
               elem(this1.contents ++ that1.contents)
               ^
<console>:22: error: not found: value elem
               elem(
               ^
<console>:30: error: not found: value elem
                  val left = elem(' ', (w - width) / 2, height)
                             ^
<console>:31: error: not found: value elem
                  var right = elem(' ', w - width - left.width, height)
                              ^
<console>:38: error: not found: value elem
                   val top = elem(' ', width, (h - height) / 2)
                             ^
<console>:39: error: not found: value elem
                   var bot = elem(' ', width, h - height - top.height)
                             ^

有谁知道为什么 1)我的对象无法识别伴随类(“扩展元素”) 2) 该方法无法在同一个类/伴侣对象中调用另一个方法?

我的班级设置如下:

  1 object Element 
  2 
  3     private class ArrayElement(
  4             val contents: Array[String]
  5             ) extends Element
  6 
  7     private class LineElement(s: String) extends Element 
  8         val contents = Array(s)
  9             override def width = s.length
 10             override def height = 1
 11     
 12 
 13     private class UniformElement(
 14             ch: Char,
 15             override val width: Int,
 16             override val height: Int
 17             ) extends Element 
 18         private val line = ch.toString * width
 19             def contents = Array.fill(height)(line)
 20     
 21 
 22     def elem(contents:  Array[String]): Element = new ArrayElement(contents)
 23 
 24     def elem(chr: Char, width: Int, height: Int): Element = new UniformElement(chr, width, height)
 25 
 26     def elem(line: String): Element = new LineElement(line)
 27 
 28
 29 import Element.elem
 30 abstract class Element 
 31     def contents:  Array[String]
 32 
 33     def width: Int = contents(0).length
 34     def height: Int = contents.length
 35 
 36     def above(that: Element): Element = 
 37         val this1 = this widen that.width
 38         val that1 = that widen this.width
 39         elem(this1.contents ++ that1.contents)
 40     
 41 
 42     def beside(that: Element): Element = 
 43         val this1 = this heighten that.height
 44         val that1 = that heighten this.height
 45         elem(
 46         for ((line1, line2) <- this1.contents zip that1.contents)
 47         yield line1 + line2)
 48     
 49 
 50     def widen(w: Int): Element =
 51        if (w <= width) this
 52        else 
 53            val left = elem(' ', (w - width) / 2, height)
 54            var right = elem(' ', w - width - left.width, height)
 55            left beside this beside right
 56        
 57 
 58     def heighten(h: Int): Element =
 59         if (h <= height) this
 60         else 
 61             val top = elem(' ', width, (h - height) / 2)
 62             var bot = elem(' ', width, h - height - top.height)
 63             top above this above bot
 64         
 65 
 66     override def toString = contents mkString "\n"
 67 

感谢您的帮助!


更新:我尝试了 :paste 而不是 :load,但仍然收到以下错误消息:

scala> :paste Element.scala
Pasting file Element.scala...
<console>:44: error: not found: value elem
               elem(this1.contents ++ that1.contents)
               ^
<console>:50: error: not found: value elem
               elem(
               ^
<console>:58: error: not found: value elem
                  val left = elem(' ', (w - width) / 2, height)
                             ^
<console>:59: error: not found: value elem
                  var right = elem(' ', w - width - left.width, height)
                              ^
<console>:66: error: not found: value elem
                   val top = elem(' ', width, (h - height) / 2)
                             ^
<console>:67: error: not found: value elem
                   var bot = elem(' ', width, h - height - top.height)
                             ^

【问题讨论】:

【参考方案1】:

浏览:help

scala> :help
...
:load <path>             interpret lines in a file
:paste [-raw] [path]     enter paste mode or paste a file

:load 似乎只是一个解释器。试试:paste

scala> :paste Element.scala
Pasting file Element.scala...
defined class Element
defined object Element

【讨论】:

刚刚添加了一个额外的部分。我试过 :paste 还是不行……你是怎么让它工作的? Nvm,知道了。我不小心删除了导入 Element.elem 文件 :)

以上是关于在Scala中使用抽象类和工厂时相互调用方法(伴随对象)的主要内容,如果未能解决你的问题,请参考以下文章

11抽象工厂模式

scala 抽象类和trait的区别

scala言语基础学习六

如何创建类型化工厂方法构造函数的类层次结构并使用抽象类型从 Scala 访问它们?

抽象工厂模式

Java设计模式(创建型:工厂方法模式+抽象工厂模式)