UPDATE [已解决]:需要将门放在嵌套的Wire
类之外(感谢Dima)。
我有一个抽象类Circuits
(在文件Circuits.scala中),它扩展了一个抽象类Gates
。在Gates
内定义了一个类Wire
(Gates和Wire都在文件Gates.scala中)。在这个类Wire
内部定义了几个函数,其中一个是orGate
。当我尝试访问orGate
内的Wire
和其他函数时,IDE会抱怨symbol not found
。我需要做些什么特别的事情才能orGate
显示Circuits
等?代码片段用于说明我的问题。
文件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)
}
}
file:Gates.scala:
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
}
}
}
答案 0 :(得分:-1)
Page
以及除orGate
和addAction
之外的大多数其他内容都应在setSignal
之外定义。
另外,你真的在这里编写java代码(虽然是scala语法)。我鼓励您查找有关scala的书籍或在线资源,并阅读前几章,以熟悉该语言的基本概念和范例。