简体   繁体   English

创建类时会调用Java构造函数吗? 未创建对象时出现“无法将构造函数应用于给定类型”错误

[英]Are Java constructors called upon creating a class? “constructor cannot be applied to given types” error when no object has been created

I'm reconstructing the following code from a tutorial. 我正在从教程中重建以下代码。 When I run complie it I get the following error: 当我运行 complie时,出现以下错误:

constructor Vierbeiner in class Vierbeiner cannot be applied to given types; Vierbeiner类中的构造函数Vierbeiner不能应用于给定类型; required: java.lang.String found: no arguments reason: actual and formal argument lists differ in length 必需:找到的java.lang.String:无自变量原因:实际和正式自变量列表的长度不同

My understanding is that this error occurs because the parent class Vierbeiner has a constructor that requires a String argument and no constructor requiring no argument. 我的理解是,发生此错误是因为父类Vierbeiner的构造函数需要String参数,而没有构造函数不需要参数。 What I don't understand is why I get this error without creating an object of class Hund. 我不明白的是为什么我没有创建Hund类的对象而收到此错误。 Why is a constructor being called when I haven't created an object of class Hund? 当我没有创建Hund类的对象时,为什么要调用构造函数? Every existing question I've seen about this error involves creating an object of the child class. 我所见过的关于此错误的每个现有问题都涉及创建子类的对象。

public class Vierbeiner {

    public static void main(String[] args){
         Vierbeiner hund = new Vierbeiner("Bello");
         hund.rennen();
    }

    String vierbeinerName;

    Vierbeiner(String pName) {
        vierbeinerName = pName;
    }

    public void rennen() {
        System.out.println(vierbeinerName+" rennt los!");
    }

}

class Hund extends Vierbeiner {
}

EDIT: In reponse to the answers I've gotten, what's still not clear to me is why the following (case 1) compiles with no problem: 编辑:为了回应我得到的答案,对我来说仍然不清楚的是为什么以下(情况1)可以毫无问题地进行编译:

public class Vierbeiner{
    public static void main(String[] args){

         Vierbeiner hund = new Vierbeiner();
         Hund hund2 = new Hund();  
         // Hund hund3 = new Hund("doggy");    
    }

   String vierbeinerName;

   Vierbeiner() {
       vierbeinerName = "test";
   };

   Vierbeiner(String pName){

   }

   }

class Hund extends Vierbeiner{

};

In which I create a Hund object seemingly using the no argument constructor which I defined in the Vierbeiner class. 在其中似乎使用在Vierbeiner类中定义的no参数构造函数创建了Hund对象。

But if I uncomment the following (case 2) I get a compilation error: 但是,如果我取消注释以下情况(情况2),则会出现编译错误:

Hund hund3 = new Hund("doggy");

In case 1, the compiler looks to the parent class Vierbeiner to find a no argument constructor to create the hund2 object. 在情况1中,编译器将在父类Vierbeiner上查找无参数构造函数以创建hund2对象。 In case 2 I would expect the compiler to do the same thing, that is go to the parent class Vierbeiner to find a string argument constructor to create the hund3 object, but this doesn't seem to be the case without using "super" and I don't understand why. 在情况2中,我希望编译器做同样的事情,那就是去父类Vierbeiner查找一个字符串参数构造函数来创建hund3对象,但是如果不使用“ super”,情况似乎并非如此。我不明白为什么。

You are receiving an error during compilation, not an error or exception during runtime. 您在编译期间收到错误,而不是运行时收到错误或异常。

This means that the code is not being executed, so no class instances have been created - there is no invocation of any constructor. 这意味着代码没有被执行,因此没有创建类实例-没有任何构造函数的调用。

During compilation, it is easy to detect that you are extending a class which has a single string argument constructor, which the extending class must call (in order to create the underlying base class). 在编译期间,很容易检测到您正在扩展的类具有单个字符串参数构造函数,扩展类必须调用该构造函数(以便创建基础基类)。

Note that once you have implemented a constructor, you can no longer rely upon the autogenerated no-parameter default constructor of your base class - you must now invoke the parameterized constructor of your base class in your deriving class. 请注意,一旦实现了构造函数, 就不能再依赖于基类的自动生成的无参数默认构造函数 -现在必须在派生类中调用基类的参数化构造函数。

Since this sort of implementation is lacking in your code - the error you described occurs. 由于您的代码中缺少这种实现,因此会发生您描述的错误。

A simple possible solution would be to provide a default constructor implementation which will invoke the single parameter constructor of the base class: 一个简单的可能解决方案是提供一个默认的构造函数实现,该实现将调用基类的单个参数构造函数:

class Hund extends Vierbeiner{
    Hund() {
        super("DefaultName");
    }    

};

This addition to your code solves the compilation time error. 代码的新增功能解决了编译时错误。

A more practical approach would be to make sure that your deriving class has a constructor with a string parameter as well, which can then be passed to the base class constructor: 一种更实用的方法是确保派生类也具有带有字符串参数的构造函数,然后可以将其传递给基类构造函数:

class Hund extends Vierbeiner{
    Hund(String name) {
        super(name);
    }    

};

References: 参考文献:

You have a compile time error. 您有一个编译时错误。 You need to add the constructor in your derived class calling the base clase constructor using the super keyword. 您需要在派生类中添加构造函数,并使用super关键字调用基本clase构造函数。

class Hund extends Vierbeiner {

    public Hund(String pName) {
        super(pName);
    }

};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java 错误:class 中的构造函数不能应用于给定类型 - Java error: constructor in class cannot be applied to given types Java 错误:class 帖子中的构造函数帖子不能应用于给定类型 - Java error: constructor posts in class posts cannot be applied to given types Java 错误:class 中的构造函数事务无法应用于给定类型 - Java error: constructor Transaction in class Transaction cannot be applied to given types (Java)错误:类MinimaxThread中的构造函数MinimaxThread无法应用于给定类型 - (Java) error: constructor MinimaxThread in class MinimaxThread cannot be applied to given types 我的Java Error构造函数在类中无法应用于给定的类型。 - My Java Error constructor, in class, cannot be applied to given types; JAVA 错误:class 中的构造函数不能应用于给定类型; - JAVA error: constructor in class cannot be applied to given types; Cube.java:19: 错误:类 Object 中的构造函数 Object 不能应用于给定类型; - Cube.java:19: error: constructor Object in class Object cannot be applied to given types; 类中的java构造函数不能应用于给定类型 - java constructor in class cannot be applied to given types java:类中的构造函数无法应用于给定类型 - java: constructor in class cannot be applied to given types 构造函数具有继承性时,“无法将构造函数应用于给定类型” - “Constructor cannot be applied to given types” when constructors have inheritance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM