关联关系练习题

本文通过一个简单的Java代码示例,展示了如何创建人(Person)与鞋子(Shoes)之间的关联关系。在TestMain主类中,创建了Person对象并为其关联了一双红色的36号鞋子。Person类包含了Shoes对象的属性,并重写了show方法以展示人和其所穿鞋子的信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:人与鞋子的关联关系???

 

创建三个类testmain为主类

package shoes;

 

public class TestMain {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

Shoes sh=new Shoes("红色",36);

Person p=new Person("李四",sh);

p.show();

 

}

 

}

 

第二个类:person

package shoes;

 

public class Person {

String name;

Shoes  sh;//关联关系

 

public Person(String name, Shoes sh) {

super();

this.name = name;

this.sh = sh;

}

 

void show(){

System.out.print(name);

System.out.print(sh);//调用ToString()  

 

 

}

 

}


第三个类:shoes

package shoes;

 

public class Shoes {

   String color;

   int size;

   

public Shoes(String color, int size) {

super();

this.color = color;

this.size = size;

}

 

@Override

public String toString() {

return "Shoes [color=" + color + ", size=" + size + "]";

}

   

  

 

 

 

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值