2012-11-22 6 views
1

자, 문제가 있습니다. 나는 그래서 '내 클래스를 확장하려면 지금클래스 + 새 매개 변수 확장

getAllMyStuff(); 

:

나는 두 번째 메소드를 호출이 클래스에서 클래스 GetStuff

public class GetStuff { 

    public GetStuff(String data) { 
     // stuff 
    } 

} 

나는 방법 getMyStuff을 (이)가 이 두 번째 클래스에서 getAllMyStuffMethod를 재정의하지만이 메서드 내부를 오버라이드합니다. 나는 생성자에서 newData의 매개 변수를 사용해야합니다 :

private String getAllMyStuffMethod() { 
    if (newData==0) // do something 
} 

어떻게 newData의 여기 사용할 수 있습니까? :(

+1

클래스 이름은 명사이어야한다. –

+0

@AdamArold이 빠른 이름을 사용하여 문제를 신속하게 노출했지만 의견을 보내 주시면 감사하겠습니다. –

답변

1
public class GetStuff { 
    public GetStuff(String data) { 
     System.out.println(data); 
    } 
} 

public class GetSecondStuff extends GetStuff { 
    private int newData; 

    public GetSecondStuff(String data, int newData) { 
     super(data); 
     this.newData = newData; 


     data = "GetSecondStuff";   
     System.out.println(data); 

     System.out.println(getAllMyStuffMethod()); 

    } 

    private String getAllMyStuffMethod() { 
     String ret=null; 
      if (this.newData==0) 
       ret="0"; 
      else 
       ret="1"; 

     return "new data : "+ret; 
    } 
} 

public class main { 

    public static void main(String[] args) {   

     GetSecondStuff gf2 = new GetSecondStuff("GetStuff",1);  
    } 

} 

출력 :

GetStuff

GetSecondStuff

새로운 데이터 : 1

+0

그 줄은 "newData = _newData;"입니다. super() ... 다음에 실행됩니다. super()를 호출 할 때이 메서드를 재정의하려고 시도하는 메서드가 실행됩니다. 따라서 newData를 사용하면 NullPointerException이 발생합니다. –

+0

잘못된 것입니다. newData는 null이 아닙니다. 시도해보십시오. 위에 수정했습니다. – Talha

1

는 인스턴스 변수에 변수 newData에 저장이 함께 유 클래스 GetSecondStuff

같은 뭔가에에 액세스 할 수 있습니다..

public class GetSecondStuff extends GetStuff { 
    private int newData; 

    public GetSecondStuff(String data, int newData) { 
     super(data); 
     this.newData = newData; 
    } 

    private String getAllMyStuffMethod() { 
     if (this.newData==0) // do something 
    } 
    } 

편집 :

을 의견 중 하나에서 나는 u가 superclass의 subclass-parameter를 사용하기를 원한다는 것을 읽었습니다. 그래서 새로운 매개 변수가 수퍼 클래스에없는 이유를 말해 줄 수 있습니까?

+0

다른 클래스에서 온 것입니다 –

+0

무슨 뜻입니까? 어쩌면 도움이 될 것입니다. u가 우리에게 무엇을 성취하고자하는지에 대한 구체적인 예를 들어 줄 수 있습니다. 그래서 우리는 더 자세한 정보를 제공 할 수 있습니까? – semTex

2

GetSecondStuff 클래스에 새 필드를 만들고이를 생성자에 지정하면됩니다. 그런 다음 재정의 된 메서드에서 newData를 사용할 수 있습니다.

+0

private String newData를 추가하면; super() ... 후에 만 ​​생성자에서 사용할 수 있습니다. getAllMyStuff()가 super()에서 호출되므로 도움이되지 않습니다. e :( 또는 i missunderstood? –

+0

기본적으로 당신의 생성자가'int'를 취합니다. – jlordo

+0

글쎄, 맞아요.하지만 이런 일이 생기면, 당신은 당신의 디자인에 대해 생각해야만합니다. 그렇지 않으면, 당신은 당신의 생성자에서 super()를 할 수 있지만, – looper

1

첫 번째 클래스를 확장하는 클래스는 자체 속성을 가질 수 있습니다.

public class GetSecondStuff extends GetStuff { 
    int _newData 
    public GetSecondStuff(String data, int newData) { 
     super(data); 
     _newData = newData; 
    } 


    private String getAllMyStuffMethod() { 
    if (_newData==0) // do something 
    } 
} 
+0

그 일은 그 _newData = newData;가 super() ... 다음에 실행되며, super()를 호출 할 때 그것을 재정의하려는 메소드가 실행됩니다. –

+0

문제가 해결되지 않습니다. 확장 기능이 아닌 기본 클래스를 수정해야하는 것 같습니다. – Sindico

관련 문제