문제
나는이 라인에 대해 약간 의심의 여지가있다 :
익명 클래스는 생성자를 정의 할 수 없습니다
그런 다음 다음 구문으로 익명 클래스를 정의 할 수있는 이유는 다음과 같습니다.
new class-name ( [ argument-list ] ) { class-body }
해결책
익명 클래스에서 생성자를 정의하지 않고 슈퍼 클래스에서 생성자를 호출합니다.
익명 클래스에 적절한 생성자를 추가 할 수는 없지만 비슷한 일을 할 수 있습니다. 즉 초기화 블록.
public class SuperClass {
public SuperClass(String parameter) {
// this is called when anonymous class is created
}
}
// an anonymous class is created and instantiated here
new SuperClass(parameterForSuperClassConstructor) {
{
// this code is executed when object is initialized
// and can be used to do many same things as a constructors
}
private void someMethod() {
}
}
다른 팁
예제는 익명을 만듭니다 아강 의 class-name
, 그리고 당신은 당신의 익명 클래스에 특정한 생성자를 만들 수 없습니다. 당신이주는 인수 목록은 class-name
건설자.
이것은 추상 클래스가 존재 함을 의미합니다 class-name
정의 된 생성자와 함께. 당신은 서브 클래스의 생성자에서 super ()를 사용하는 것과 유사한 익명 클래스에서 해당 생성자를 사용하고 있습니다.
제휴하지 않습니다 StackOverflow