Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 类中没有定义具有3个参数的构造函数?_Java_Xml_Spring - Fatal编程技术网

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

Java 类中没有定义具有3个参数的构造函数?,java,xml,spring,Java,Xml,Spring,让我澄清一下,我是Spring框架的初学者 我有三个类文件,现在在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

让我澄清一下,我是
Spring框架
的初学者

我有三个类文件,现在在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());
    }
}
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();
    }
}
这是我的
MainProgram.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());
    }
}
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
文件中得到一个错误,因为
类中没有定义3个参数的构造函数

请帮忙,那是什么意思

当然,我们会非常感谢您的帮助

public void Address(String city, String state, String address)
应该是

public MyAddress(String city, String state, String address)
构造函数中的类名错误,而且构造函数没有返回类型

对于
员工
,您也有类似的错误:

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

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

应该是

public MyAddress(String city, String state, String address)
构造函数中的类名错误,而且构造函数没有返回类型

对于
员工
,您也有类似的错误:

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

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

address类有一个默认构造函数。从方法中省略
void
关键字。

address类具有默认构造函数。从方法中省略
void
关键字。

如定义, “类包含被调用以从类蓝图创建对象的构造函数。构造函数声明类似于方法声明,只是它们使用类的名称,并且没有定义的返回类型”


“类包含被调用以从类蓝图创建对象的构造函数。构造函数声明类似于方法声明,只是它们使用类的名称,并且在
MyAddress
类中没有返回类型“

,而不是创建构造函数。您创建了
Address
方法,
public void Address(…)
更改为
public MyAddress(…)
将使其在
MyAddress
类中工作,而不是创建一个构造函数您创建了
Address
方法,
public void Address(…)
更改为
public MyAddress(…)
将使其正常工作

是的!!你是对的!!多么愚蠢的错误…:p谢谢……;)是 啊你是对的!!多么愚蠢的错误…:p谢谢……;)java so
public Employee中没有构造函数的返回类型(int-id,字符串名称,MyAddress地址){
java so
public Employee中没有构造函数的返回类型(int-id,字符串名称,MyAddress地址){