Brand接口和实现类
/**
* 品牌
* @author Administrator
*
*/
public interface Brand {
void sale();
}
class Lenovo implements Brand {
@Override
public void sale() {
System.out.println("销售联想电脑");
}
}
class Dell implements Brand {
@Override
public void sale() {
System.out.println("销售Dell电脑");
}
}
class Shenzhou implements Brand {
@Override
public void sale() {
System.out.println("销售神舟电脑");
}
}
Computer2类
/**
* 电脑类型的维度
* @author Administrator
*
*/
public class Computer2 {
protected Brand brand;
public Computer2(Brand b) {
this.brand = b;
}
public void sale(){
brand.sale();
}
}
class Desktop2 extends Computer2 {
public Desktop2(Brand b) {
super(b);
}
@Override
public void sale() {
super.sale();
System.out.println("销售台式机");
}
}
class Laptop2 extends Computer2 {
public Laptop2(Brand b) {
super(b);
}
@Override
public void sale() {
super.sale();
System.out.println("销售笔记本");
}
}
Client测试类
public class Client {
public static void main(String[] args) {
//销售联想的笔记本电脑
Computer2 c = new Laptop2(new Lenovo());
c.sale();
//销售神舟的台式机
Computer2 c2 = new Desktop2(new Shenzhou());
c2.sale();
}
}