내가 가지고 있다고 가정 해 봅시다.
class Superclass {
//fields...
methodA() {...}
methodB() {...}
...
}
class Subclass extends Superclass {
//fields...
methodA() {
// Here I need to call methods A and B from superclass:
// For this, I can use supper
super.methodA();
super.methodB();
// Or I have to instantiate the superclass and use the instance
Superclass superclass = new Superclass();
superclass.methodA();
superclass.methodB();
}
두 가지 방식으로 작동하지만 어느 것이 더 나은지 알고 싶습니다. 이러한 방법 중 어떤 것이 잘못된 프로그래밍 기술입니까? 나는 당신이 나에게 대답과 논쟁을 주길 바랍니다.
super.methodA();
Superclass superclass = new Superclass();
superclass.methodA();
이 두 methodA 호출은 서로 다른 인스턴스에서 작동하므로 완전히 다릅니다. 현재 인스턴스에서 super.methodA()
실행 methodA
됩니다. 현재 인스턴스와 관련이없는 새 인스턴스에서 superclass.methodA()
실행 methodA
됩니다 Superclass
.
거의 항상 첫 번째 옵션을 사용합니다. 두 번째 옵션은 새 인스턴스를 만들고 해당 인스턴스에서 메서드를 호출 한 다음 해당 인스턴스에 대해 아무것도 수행하지 않는 것은 의미가 없습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다