class 中没有定义具有 3 个参数的构造函数?

No constructor with 3 arguments defined in class?

让我澄清一下,我是 Spring framework 的初学者。

我有三个 class 文件,现在我在 beans.xml 中遇到错误。你可以看看我的代码。

这里是MyAddress.java:

package com.project;

public class MyAddress {
    private String city;
    private String state;
    private String address;

    public void Address(String city, String state, String address){
        this.city=city;
        this.state=state;
        this.address=address;
    }

    public String toString(){
        return city+" "+state+" "+address;
    }
}

这是我的 Employee.java

package com.project;

public class Employee {
    private int id;
    private String name;
    private MyAddress address;

    public Employee(){
        System.out.print("Default constructor..");
    }

    public void Employee(int id, String name, MyAddress address){
        this.id=id;
        this.name=name;
        this.address=address;
    }

    public void show(){
        System.out.println(id+" "+name);
        System.out.println(address.toString());
    }
}

这是我的MainProgram.java

package com.project;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

        ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");

        Employee em=(Employee)ac.getBean("e");

        em.show();
    }
}

最后是我的 beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="e" class="com.project.MyAddress">
        <constructor-arg value="USA" type="String"></constructor-arg>
        <constructor-arg value="Delhi" type="String"></constructor-arg>
        <constructor-arg value="Bangalore" type="String"></constructor-arg>
    </bean>

    <bean id="e2" class="com.project.Employee">
        <constructor-arg value="123" type="int"></constructor-arg>
        <constructor-arg value="raj"></constructor-arg>
        <constructor-arg>
            <ref bean="e"/>
        </constructor-arg>
    </bean>

</beans>

我在 beans.xml 文件中收到错误 No constructor with 3 arguments defined in class

求助,什么意思?

当然,如果有帮助,我们将不胜感激!!

这个

public void Address(String city, String state, String address)

应该是

public MyAddress(String city, String state, String address)

您的构造函数中的 class 名称错误,此外,构造函数没有 return 类型。

您对 Employee 有类似的错误:

public void Employee(int id, String name, MyAddress address)

应该是

public Employee(int id, String name, MyAddress address)

地址class有一个默认的构造函数。从方法中省略 void 关键字。

定义为here, "A class contains constructors that are invoked to create objects from the class blueprint. Constructor declarations look like method declarations—except that they use the name of the class and have no return type"

MyAddress class 中,您没有创建构造函数,而是创建了 Address 方法, 将 public void Address(...) 更改为 public MyAddress(...) 将使其工作