2017-01-30 5 views
1

나는 내 코드에서로드 블록을 쳤다. 이것은 손에있는 수업입니다."상속 된 추상 메소드 java.awt.event.ActionListener.actionPerformed (java.awt.event.ActionEvent)"를 구현해야 함은 무엇을 의미합니까?

public class StartRoom extends Room implements ActionListener { 

    JButton buttonTwo; 

    public StartRoom() { 
     start(); 
     buttonOne = new JButton("Go to door."); 
     buttonTwo = new JButton("Look at skeleton."); 
     label = new JLabel("You walk into the dungeon, the room is covered with vines. There is a skeleton sitting near the northern door. What do you do?"); 
     panelOne.add(label); 
     panelOne.add(buttonOne); 
     buttonOne.addActionListener(this); 
     buttonTwo.addActionListener(this); 
    } 

    class MyActionListener implements ActionListener { 
     @Override 
     public void actionPerformed(java.awt.event.ActionEvent ae) { 

     } 
    } 

    public static void main(String[]args) { 
     new StartRoom(); 
    } 
} 

그것은 타입 StartRoom 줄 오에 상속 된 추상 메소드 java.awt.event.ActionListener.actionPerformed(java.awt.event.ActionEvent)를 구현해야합니다,하지만 난 그것을 물어 무엇을 알아낼 수 없다는!

+1

ActionListener에서 추상 메소드'actionPerformed (ActionEvent)'를 상속해야한다는 것입니다. 나는 이것이 오류를 읽을 때 아주 분명하다고 생각합니다. 여기서 용어를 이해하지 못하면 OO를 완전히 이해하지 못했을 것입니다. 그래서 그것에 대해 다시 읽으십시오 – AxelH

+0

왜 이렇게 많은 upvotes가 있습니까? 질문자는 인터페이스를 구현하는 것을 분명히 잊어 버렸습니다. 아마도 친구들의 업주레, 어쩌면? 아니면 나는 단지 미신적 인/편집증적일뿐입니다. – byxor

답변

2

StartRoom implements ActionListenerStartRoomActionListener으로 계약해야합니다. 방법 actionPerformed(ActionEvent)은 직접 구현해야합니다.

public class StartRoom extends Room implements ActionListener { 
    ... 

    @Override 
    public void actionPerformed(java.awt.event.ActionEvent ae) { 
     // your code here.... 
    } 
} 

당신이 MyActionListener의 인스턴스 this 교체, 예를 들어, 당신은 buttonTwo.addActionListener(this);의 사용을 변경해야 할 다른 클래스, MyActionListener에 위임 할 경우

. 후자의 경우에

MyActionListener toto = new MyActionListener(); 
buttonTwo.addActionListener(toto); 

당신은 StartRoom 클래스 선언에서 implements ActionListener을 제거 mshould.

0

무언가를 구현할 때 해당 클래스를 인터페이스로 사용하고 있습니다. 즉, 구현중인 클래스의 모든 메소드를 사용하고 다시 정의해야합니다. 그러나, 당신이 그 클래스의 메소드를 그대로 사용하고 싶다면 클래스의 메소드를 그대로 사용하고 클래스의 메소드를 확장 할 수 있습니다. 하나의 클래스 만 확장 할 수 있지만 많은 수의 클래스를 인터페이스로 구현할 수 있습니다.