Java装配模式

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);
	}
}
张三是一个长的高高的、重重的的人
李四是一个长的高高的、瘦瘦的的人
true

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值