gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:13:35 25 4
gpt4 key购买 nike

让我澄清一下,我完全是 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());
}
}

这是我的 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)

你的构造函数中的类名错误,此外,构造函数没有返回类型。

Employee 也有类似的错误:

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

应该是

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

关于java - 没有在类中定义具有 3 个参数的构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27921047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com