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 sopublic Employee中没有构造函数的返回类型(int-id,字符串名称,MyAddress地址){
java sopublic Employee中没有构造函数的返回类型(int-id,字符串名称,MyAddress地址){