2016-11-19 1 views
-3

업데이트 [적용됨] : 중첩 된 외부 게이트를 넣으려면 Wire 클래스 (감사합니다 디마)가 필요합니다.추상 클래스 내에 중첩 된 클래스의 메소드에 액세스하는 방법은 무엇입니까?

편집 : 나는 왜 내가이 질문에 대해 downvoted인지 모르겠다. Martin Odersky의 Reactive Programming에서 온라인 coursera 과정을 수강하고 있기 때문에 합법적 인 질문입니다. 강의 3.6 "개별 이벤트 시뮬레이션 구현 및 테스트"를 살펴보면 Martin이 코드를 다음과 같이 정확히 구현 한 것을 알 수 있습니다. 나는 그랬고 그는 이런 실수를하지 않았다. 이것은 과제가 아니며 모든 코드가 제시되었으며 간단히 슬라이드에서 복사했습니다. 실제 비디오의 스크린 샷을 보면 내부에의 Wire 클래스로 작성된 게이트가 표시되어 있습니다. enter image description here

enter image description here

나는 추상 클래스 Gates를 확장 (파일 Circuits.scala에서) 추상 클래스 Circuits 있습니다. Gates 안에 클래스 Wire이 정의되어 있습니다 (Gates와 Wire는 모두 Gates.scala 파일에 있음). 그리고이 클래스의 내부에는 Wire이 정의되어 있으며 그 중 하나는 orGate입니다. orGate에 액세스하려고 시도하고 Wire 내부의 다른 기능을 시도하면 IDE는 symbol not found으로 불평합니다. Circuits에서 orGate 등을 볼 수 있도록 특별한 조치를 취해야합니까? 내 문제를 설명하는 코드 스 니펫이 다음과 같습니다.

파일 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) 
     } 

    } 

파일 : Gates.scala은 : addActionsetSignal 제외

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 
    } 


    } 
} 

답변

-1

orGate 및 대부분의 다른 것들, Wire의 외부에서 정의되어야한다.

또한 여기에 자바 코드를 작성하고 있습니다 (스칼라 구문 임에도 불구하고). 스칼라에 대한 책이나 온라인 리소스를 찾고 처음 몇 장을 읽으면 언어의 기본 개념과 패러다임을 익힐 수 있습니다.

+0

나는 Coursera의 코스 슬라이드에서이 코드를 도매로 복사했으며 강사는 코드를있는 그대로 실행할 수있었습니다. – thetrystero

+0

글쎄, 그는, 당신은 ... 당신의 코드 복사 기술에 대해 무엇을 말합니까? ;) – Dima

+0

그래서'''orGate''와 다른 대부분의 것들은''''''''의 외부에 정의되어야한다고 당신의 의견을 되 찾으십니까? – thetrystero

관련 문제