package com.gary.people.core;
import com.gary.people.interfaces.IHeight;
import com.gary.people.interfaces.IWeight;
public class People {
private String name;
private IHeight height;
private IWeight weight;
public People() {
}
public People(String name) {
setName(name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setHeight(IHeight height) {
this.height = height;
}
public void setWeight(IWeight weight) {
this.weight = weight;
}
@Override
public String toString() {
return name + "是一个长的" + height.height() + "、" + weight.weight() + "的人";
}
}
两个接口
public interface IHeight {
String height();
}
public interface IWeight {
String weight();
}
public class ShortHeight implements IHeight {
@Override
public String height() {
return "矮矮的";
}
}
public class TalHeight implements IHeight {
@Override
public String height() {
return "高高的";
}
}
public class HeavyWeight implements IWeight {
@Override
public String weight() {
return "重重的";
}
}
public class LightWeight implements IWeight {
@Override
public String weight() {
return "轻轻的";
}
}
public class Test {
public static void main(String[] args) {
People people1 = new People("张三");
people1.setHeight(new TalHeight());
people1.setWeight(new HeavyWeight());
System.out.println(people1);
People people2 = new People("李四");
people2.setHeight(new TalHeight());
people2.setWeight(new IWeight() {
@Override
public String weight() {
return "瘦瘦的";
}
});
System.out.println(people2);
ShortHeight sh = new ShortHeight();
System.out.println(sh instanceof IHeight);
}
}