题目:人与鞋子的关联关系???
创建三个类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 + "]";
}
}