Scala 如何访问嵌套在抽象类中的类中的方法?

Scala 如何访问嵌套在抽象类中的类中的方法?,scala,Scala,更新[已解决]:需要将闸门置于嵌套Wire类之外(感谢Dima) 编辑:我不知道为什么我在这个问题上被否决了。这是一个合法的问题,因为我正在学习Martin Odersky的在线课程反应式编程,如果你亲自去看第3.6课“离散事件模拟实现和测试”,你会发现Martin实现的代码与我完全一样,他没有得到这些错误。这不是一个作业,所有的代码都是呈现出来的,我只是从幻灯片上复制过来的。您可以从实际视频中看到下面的屏幕截图,显示门是在Wire类内部编写的 我有一个抽象类Circuits(在Circui

更新[已解决]:需要将闸门置于嵌套
Wire
类之外(感谢Dima)

编辑:我不知道为什么我在这个问题上被否决了。这是一个合法的问题,因为我正在学习Martin Odersky的在线课程反应式编程,如果你亲自去看第3.6课“离散事件模拟实现和测试”,你会发现Martin实现的代码与我完全一样,他没有得到这些错误。这不是一个作业,所有的代码都是呈现出来的,我只是从幻灯片上复制过来的。您可以从实际视频中看到下面的屏幕截图,显示门是在Wire类内部编写的

我有一个抽象类
Circuits
(在Circuits.scala文件中),它扩展了一个抽象类
Gates
。在
Gates
中定义了一个类
Wire
(Gates和Wire都在文件Gates.scala中)。在这个类中,
Wire
定义了几个函数,其中一个是
orGate
。当我试图访问
orGate
Wire
中的其他功能时,IDE会抱怨找不到
符号
。我需要做什么特殊的事情才能从
电路中看到
组织
等?下面是说明我的问题的代码片段

文件Circuits.scala:

package week3      
abstract class Circuits extends Gates {

      def halfAdder(a: Wire, b: Wire, s: Wire, c: Wire) {
        val d, e = new Wire
        orGate(a, b, d)      // <--- symbol not found: orGate
        andGate(a, b, c)     // <---- symbol not found: andGate 
        inverter(c, e)       // <--- etc.
        andGate(d, e, s)
      }

      def fullAdder(a: Wire, b: Wire, cin: Wire, sum: Wire, cout: Wire) {
        val s, c1, c2 = new Wire
        halfAdder(a, cin, s, c1)
        halfAdder(b, s, sum, c2)
        orGate(c1, c2, cout)
      }

    }

orGate
和除
addAction
setSignal
之外的大多数其他内容都应在
Wire
之外定义


此外,您确实需要在这里编写java代码(尽管是使用scala语法)。我鼓励您查找有关scala的书籍或在线资源,并通读前几章,以熟悉该语言的基本概念和范例。

我从Coursera课程的幻灯片中大量复制了此代码,讲师能够按原样运行代码。好吧,他是,你不是。。。关于你的代码复制技能,它告诉了你什么那么,你是否收回你的评论,认为
orGate
和大多数其他东西应该在
Wire
之外定义,或者你仍然支持它?@Therystero我支持它。我不知道你从哪里复制了什么,但是你在这里发布的代码与我描述的方式不一样。好吧,很公平。我来试试这个。现在可能太晚了。谢谢
package week3


abstract class Gates extends Simulation {

  def InverterDelay: Int
  def AndGateDelay: Int
  def OrGateDelay: Int

  class Wire {

    private var sigVal = false
    private var actions: List[Action] = List()

    def getSignal: Boolean = sigVal
    def setSignal(s: Boolean): Unit = {
      if (s != sigVal) {
        sigVal = s
        actions foreach (_())
      }
    }
    def addAction(a: Action): Unit = {
      actions = a::actions
      a()
    }

    def inverter(input: Wire, output: Wire): Unit = {
      def invertAction(): Unit = {
        val inputSig = input.getSignal
        afterDelay(InverterDelay) { output setSignal !inputSig}
      }
      input addAction invertAction
    }


    def andGate(in1: Wire, in2: Wire, output: Wire): Unit = {
      def andAction(): Unit = {
        val in1Sig = in1.getSignal
        val in2Sig = in2.getSignal
        afterDelay(AndGateDelay) {output setSignal (in1Sig & in2Sig)}
      }

      in1 addAction andAction
      in2 addAction andAction
    }


    def orGate(in1: Wire, in2: Wire, output: Wire): Unit = {
      def orAction(): Unit = {
        val in1Sig = in1.getSignal
        val in2Sig = in2.getSignal
        afterDelay(OrGateDelay) {output setSignal (in1Sig | in2Sig)}
      }

      in1 addAction orAction
      in2 addAction orAction
    }

    def probe(name: String, wire: Wire): Unit = {
      def probeAction(): Unit = {
        println(name, currentTime, wire.getSignal)
      }
      wire addAction probeAction
    }


  }
}