老师请检查!
package lean2;
public class Clothes {
private String style;
private String color;
public Clothes() {
}
public Clothes(String style, String color) {
this.style = style;
this.color = color;
}
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Clothes{" +
"style='" + style + '\'' +
", color='" + color + '\'' +
'}';
}
}
package lean2;
public class Person {
private String name;
private Clothes clothes;
public Person() {
}
public Person(String name, Clothes clothes) {
this.name = name;
this.clothes = clothes;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Clothes getClothes() {
return clothes;
}
public void setClothes(Clothes clothes) {
this.clothes = clothes;
}
public void dress(){
System.out.println(this.getName()+"爱穿"+this.getClothes().getColor()+this.getClothes().getStyle());
}
}
package lean2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:lean2.xml");
Person p1 = context.getBean("p1", Person.class);
p1.dress();
Person p2 = context.getBean("p2", Person.class);
p1.dress();
}
}
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="c1" class="lean2.Clothes">
<property name="color" value="红色"/>
<property name="style" value="连衣裙"/>
</bean>
<bean id="c2" class="lean2.Clothes">
<property name="color" value="蓝色"/>
<property name="style" value="小西装"/>
</bean>
<bean id="p1" class="lean2.Person">
<property name="name" value="女孩"/>
<property name="clothes" ref="c1"/>
</bean>
<bean id="p2" class="lean2.Person">
<property name="name" value="男孩"/>
<property name="clothes" ref="c2"/>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>untitled</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
</dependencies>
</project>
32
收起
正在回答 回答被采纳积分+1
1回答
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星