老师请检查!

老师请检查!

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>


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕小班 2021-09-12 14:59:03

同学你好,运行同学的代码并没有问题,只是在测试方法中,注意要调用两个对象的dress方法,而不是只调用p1对象的方法,比如

https://img1.sycdn.imooc.com//climg/613da50e092a290e11000434.jpg

祝学习愉快~​

  • 提问者 残泪 #1

    好的,没注意到,谢谢老师指正

    2021-09-12 15:45:09
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师