错误:类Name2中的构造函数Name2不能应用于给定类型。

问题描述 投票:0回答:1

在编码时我遇到了一些问题.在多级继承中, 是在A类和B类中使用的变量,但当运行程序时,它显示错误。我把代码粘贴在下面。

     class Name{
    int cl=0;
}
class Name2 extends Name{
    public Name2(int cl)
    {
        this.cl=cl;
    }
    public String toString()
    {
        return String.valueOf(cl);
    }
}
class Name3 extends Name2{
    int tl;
    public Name3(int cl,int tl)
    {
        super();
        this.tl=tl;
    }
    public String toString()
    {
        return String.valueOf(cl);
    }
}

public class HelloWorld{

     public static void main(String []args){
        Name3 obj=new Name3(3,6);

        System.out.println(obj);
     }
}



  ----------------------------------

Output is:

    > $javac HelloWorld.java HelloWorld.java:18: error: constructor Name2 in
    > class Name2 cannot be applied to given types;
    >         super();
    >         ^   required: int   found: no arguments   reason: actual and formal argument lists differ in length 1 error
java inheritance polymorphism abstract super
1个回答
1
投票

正如错误所说,你需要给construtor中的super()传递一个int参数。

public Name3(int cl,int tl)
    {
        super(cl);
        this.tl=tl;
    }
© www.soinside.com 2019 - 2024. All rights reserved.