2013-10-16 33 views
2

一点背景:的Java:扩展类,子类的构造函数给出了错误

有三类涉及:,DNASequence(object)ProteinDNA(subclass of DNASequence)。所有三个都在同一个包中。

ProteinDNA构造函数接受一个对象DNASequence和整数

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

编译类ProteinDNA给我一个错误的构造函数。

Eclipse中的错误是:

"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" 

我在做什么错?测试程序类将ProteinDNA与适当构建的DNASequence实例一起提供。

+5

'ProteinDNA''就是'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)修改子类的构造函数调用父类的构造类似下面,

public ProteinDNA(DNASequence dna, int startAt){ 

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

它看起来像你试图传递一个DNASequence对象和发生故障的事情是,物体的建设。

要求:字符串
发现:没有参数

这让我觉得,你可能尝试做财产以后类似如下:

new ProteinDNA(new DNASequence(), num); 

,编译器说,它预期一个字符串代替:

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

这有道理吗?

也许如果您发布一些特定的代码,也就是我们可以提供更多的帮助:

  • ProteinDNA构造函数调用
  • DNASequence构造函数签名
  • 测试方法的代码

而且,你能澄清为什么,如果蛋白质DNA是DNASequence的一个子类,您是否将DNASequence传递给它的构造函数?那是一种防御性的副本吗?

此外,在备选答案中提到,您可能希望将电话添加到超级构造函数(DNASequence(String))孩子的构造,作为第一线,像这样:

super("SOME STRING") 

但是,这真的取决于你的逻辑...