2013-10-16 2 views
2

에게 약간의 배경을 제공합니다자바 : 클래스 확장, 서브 클래스의 생성자는 오류

관련된 세 가지 클래스가 있습니다 Tester(main method), DNASequence(object)ProteinDNA(subclass of DNASequence)가. 세 명 모두 같은 패키지하에 있습니다.

ProteinDNA의 생성자는 생성자에서 나에게 오류를 제공 객체 DNASequence 및 클래스 ProteinDNA 컴파일 정수

public class ProteinDNA extends DNASequence{ 
public ProteinDNA(DNASequence dna, int startAt){ //this is the constructor 

을 받아들입니다.

이클립스의 오류는 다음과 같습니다

"Implicit super constructor `DNASequence()` is undefined. 
Must explicitly invoke another constructor" 

jGrasp에 오류가 있습니다 : 내가 잘못 뭐하는 거지

ProteinDNA.java:16: error: 
    constructor DNASequence in class DNASequence cannot be applied to given types; 
public ProteinDNA(DNASequence dna, int startAt)^{ 


    required: String 

    found: no arguments 

    reason: actual and formal argument lists differ in length" 

? 테스터 클래스는 적절하게 구성된 인스턴스 DNASequenceProteinDNA에 제공합니다.

+5

'ProteinDNA' _is a_'DNASequence', 그래서 그것은 DNASequence 생성자를 명시 적으로 호출 할 필요가 있습니다. –

+0

http://stackoverflow.com/questions/9143317/java-inheritance-error-implicit-super-constructor-is-undefined – Kojotak

+0

DNASequence에서 인수가없는 생성자를 생성하면 문제가 해결됩니다! 고맙습니다! – user1766889

답변

1
Parent Class DNASequence has existing constructor with parameters. There 2 solutions for this. 

1) 기본값 없음 인수 생성자를 DNA 시퀀스 클래스에 추가 할 수 있습니다.

2) 당신이 DNASequence의 개체를 전달하는 것을 시도하고 실패하는 것은 해당 개체의 건물입니다처럼 보이는

public ProteinDNA(DNASequence dna, int startAt){ 

    super(....); // This should be the 1st line in constructor code, add parameters 
       as per parent constructor 
} 
0

, 아래와 같이 부모 클래스의 생성자를 호출하는 자식 클래스의 생성자를 수정합니다.

이 필요합니다 : 찾을 문자열
:이 날 당신은 아마 다음과 같이 뭔가를해야만 수행하려고 생각하게 인수

:

new ProteinDNA(new DNASequence(), num); 

그리고 컴파일러는 기대하고 있다고을 대신 문자열 :

new ProteinDNA(new DNASequence("SOME STRING"), num);

의미가 있습니까? 당신은 즉, 일부 특정 코드를 게시 할 경우

어쩌면 우리가 더 많은 도움이 될 수 :

  • ProteinDNA 생성자 호출
  • DNASequence 생성자 서명 또한

  • 시험 방법의 코드는, 당신은 할 수 있습니다 ProteinDNA가 DNASequence의 하위 클래스 인 경우 왜 DNASequence를 해당 생성자에 전달하는지 명확하게 설명하십시오. 그것은 어떤 종류의 방어적인 사본입니까?

    super("SOME STRING") 
    

    을하지만 정말에 따라 달라집니다 다른 답변에서 언급 한

    또한, 당신은 다음과 같이 첫 번째 라인으로, 자식 생성자에 슈퍼 생성자 (DNASequence(String))에 호출을 추가 할 수 있습니다 당신의 논리 ...

  • 관련 문제