Java 为子类传入构造函数时出现“无法解析匹配的构造函数”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21583399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:30:07  来源:igfitidea点击:

"Could not resolve matching constructor" error when passing in constructor-arg for child class

javaspringinheritanceconstructor

提问by dnc253

I have the following classes:

我有以下课程:

public abstract class ParentClass
{
    public ParentClass()
    {
        throw new RuntimeException("An ID must be specified.");
    }

    public ParentClass(String id)
    {
        this(id, DEFUALT_ARG_VALUE);
    }

    public ParentClass(String id, int anotherArg)
    {
        this.id = id;
        //stuff with anotherArg
    }

    public abstract void doInstanceStuff();
}

public class ChildClass extends ParentClass
{
    @Override
    public void doInstanceStuff()
    {
        //....
    }
}

In my application context I have this:

在我的应用程序上下文中,我有这个:

<bean id="myChildInstance" class="com.foo.bar.ChildClass " scope="singleton">
    <constructor-arg value="myId" />
</bean>

The problem is that when the server starts up I get the following error:

问题是当服务器启动时,我收到以下错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ivpluginHealthCheckTest' defined in ServletContext resource [/WEB-INF/spring/root-context.xml]: Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)

Seeing the error, I tried adding the different attributes, but with no luck. I ended up with something like this:

看到错误,我尝试添加不同的属性,但没有运气。我最终得到了这样的结果:

<bean id="myChildInstance" class="com.foo.bar.ChildClass " scope="singleton">
    <constructor-arg value="myId" index="0" type="java.lang.String" name="id" />
</bean>

And I still get the same error.

我仍然遇到同样的错误。

I tried adding the same constructors to my child class, and just call super()with the appropriate arguments for each one, and that seems to fix it. However, I don't want to have to add the same constructors in all my child instances and have to maintain those with the parent class.

我尝试将相同的构造函数添加到我的子类中,并super()为每个类调用适当的参数,这似乎解决了它。但是,我不想在我的所有子实例中添加相同的构造函数,并且必须在父类中维护这些构造函数。

Is there some reason Spring has trouble calling an inherited constructor to instantiate the class? Is there something I can do to make this work?

Spring 调用继承的构造函数来实例化类是否有问题?有什么我可以做的事情吗?

采纳答案by Rohit Jain

calling an inherited constructor to instantiate the class?

调用继承的构造函数来实例化类?

Constructors are never inherited, and it doesn't really make sense. A constructor just initializes the state in that particular class. You can't expect a constructor of Parentto initialize the state of Childclass. That's the job of constructor in Childclass only.

构造函数永远不会被继承,这也没有任何意义。构造函数只是初始化该特定类中的状态。您不能期望的构造函数Parent来初始化Child类的状态。这Child只是类中构造函数的工作。

So, no you can't do what you are trying to do. And that's not the issue with Spring. That's pretty much fundamental.

所以,不,你不能做你想做的事。这不是 Spring 的问题。这是非常基本的。

回答by André Stannek

Short answer: Constructors are not inherited in Java.

简短回答:构造函数在 Java 中不是继承的。

From the JLS:

JLS

Constructor declarations are not members. They are never inherited and therefore are not subject to hiding or overriding. 

This means you have to declare the constructors needed for each subclass and call the corresponding super constructor. Even if it has the same signature it doesn't count as overriding.

这意味着您必须声明每个子类所需的构造函数并调用相应的超级构造函数。即使它具有相同的签名,也不算作覆盖。

回答by Kumar Abhinav

If you do not provide a constructor in your child class,the compiler inserts a no-arg constructor and adds the first line as call to super no-arg constructor So your code becomes :-

如果您没有在子类中提供构造函数,编译器会插入一个无参数构造函数并将第一行添加为对超级无参数构造函数的调用,因此您的代码变为:-

public class ChildClass extends ParentClass {

        public ChildClass() {
            super();
        }
    }

Obviously, u have thrown a null pointer exception which may be causing the problem.I suggest you to add a constructor and make a call to super constrcutor which takes String argument:-

显然,你抛出了一个空指针异常,这可能会导致问题。我建议你添加一个构造函数并调用带有字符串参数的超级构造函数:-

public class ChildClass extends ParentClass {
        public ChildClass(String id) {
            super(id);
        }

        @Override
        public void doInstanceStuff() {
            // ....
        }
    }

This should solve all your problems...

这应该可以解决你所有的问题......

Note:- If you add a argument constructor,the compiler doesn't not add a default constructor for you.

注意:- 如果您添加参数构造函数,编译器不会为您添加默认构造函数。

Now your beans will initialize properly as it will call the string argument constructor. Currently the compiler is providing a default no-arg constrcutor for you which in turn is calling your no arg parent class constructor and is throwing a null pointer exception..

现在您的 bean 将正确初始化,因为它将调用字符串参数构造函数。目前,编译器为您提供了一个默认的无参数构造函数,它反过来调用您的无参数父类构造函数并抛出空指针异常..