2012-04-25 3 views
2

컴파일러가 호출 할 생성자를 알 수 없기 때문에 인수가없는 생성자는 오류를 발생시킵니다. 해결 방안은 무엇인가?null 인수가있는 오버로드 된 생성자 호출

private Test() throws Exception { 
    this(null);//THIS WILL THROW ERROR, I WAN'T TO CALL A SPECIFIC CONSTRUCTOR FROM THE TWO BELOW. HOW TO DO?? 
} 
private Test(InputStream stream) throws Exception { 

} 



private Test(String fileName) throws Exception { 

} 
+0

당신이 정말 이해가되지 않기 때문에 작동하지 않습니다하려고 노력하고 있습니다. 인수로 null을 가진 생성자는 어떤 종류의 동작을 기대합니까? – posdef

답변

5

캐스트 null :

private Test() throws Exception { 
    this((String)null); // Or of course, this((InputStream)null); 
} 

그러나 당신이 null 인수 Test(String) 또는 Test(InputStream)를 호출 할 거라고 조금 이상한 것 같다 ...

1

이해가 안 왜 그렇게 사랑스럽게 만들어진 모든 생성자들은 사적입니다.

나는 이런 식으로 할 거라고 :

private Test() throws Exception { 
    this(new PrintStream(System.in); 
} 

private Test(InputStream stream) throws Exception { 
    if (stream == null) { 
     throw new IllegalArgumentException("input stream cannot be null"); 
    } 
    // other stuff here. 
}  

private Test(String fileName) throws Exception { 
    this(new FileInputStream(fileName)); 
} 
관련 문제