android_griezmann :
이 질문은 유효 또는 내가 부모 클래스의 구조를 정의하는 뭔가 잘못하고있는 중이 야 나도 몰라.
그러나 다음은 클래스의 형성하고 인터페이스입니다.
public interface Test {
public void print();
public void write();
}
class Parent implements Test{
@Override
public void print() {
System.out.print("This is parent class");
}
@Override
public void write() {
System.out.print("This is write method in parent class");
}
}
class Child extends Parent{
@Override
public void print(){
System.out.print("This is child class);
}
}
나는 인터페이스를 사용하여 메소드를 호출 예상 출력
Test test = new Parent();
test.print();
그것은 자식 클래스에서 인쇄 방법을 호출해야합니다.
그리고 인터페이스를 사용하여 메서드를 호출 할 때
Test test = new Parent();
test.write();
그것은 부모 클래스의 write 메소드를 호출해야합니다.
그래서 지금은 모두의 경우는 상위 클래스의 메소드를 부르고, 일이 아닙니다.
어떤 제안이나 답변을 많이 감사합니다.
데이브 :
사용하여:
Test test = new Parent();
test.write();
당신은 test
유형 인 Parent
및 인식하지 못합니다 Child
. 따라서 귀하의 출력에 두 가지 방법을 나타냅니다 Parent
클래스라고합니다.
시험:
Test test = new Child();
test.print(); // Will call Child::print()
test.write(); // Will call Parent::write()
당신은 당신이 원하는 무엇을 달성해야한다.
NB는 일이 들어, 당신은 추가해야합니다 write()
당신에 Test
따라서, 인터페이스 :
public interface Test {
public void print();
public void write(); // This is required for it to be accessible via the interface
}