建立一个父类
1 package chouxiang; 2 3 public abstract class Cat { 4 5 6 }
两个产品继承于父类
1 package chouxiang; 2 3 public class BMW extends Cat{ 4 5 public BMW(int num){ 6 System.out.println("你买了几辆宝马"+num); 7 } 8 9 }
1 package chouxiang; 2 3 public class Bennz extends Cat { 4 5 public Bennz(int num){ 6 System.out.println("你买了几辆奔驰"+num); 7 } 8 9 }
建立赠品的父类
1 package chouxiang; 2 3 public abstract class Fruit { 4 abstract void getColor(); 5 }
建立两个赠品
1 package chouxiang; 2 3 public class Apple extends Fruit { 4 5 @Override 6 void getColor() { 7 System.out.println("送你一箱苹果"); 8 } 9 10 }
1 package chouxiang; 2 3 public class Pearsr extends Fruit { 4 5 @Override 6 void getColor() { 7 System.out.println("送你一箱鸭梨"); 8 } 9 10 }
建立抽象类
1 package chouxiang; 2 3 public abstract class IsCat { 4 5 public abstract Cat busd(int i); 6 public abstract Fruit sendFruit(); 7 8 }
建立产品类型
1 package chouxiang; 2 3 public class ScheanA extends IsCat { 4 5 @Override 6 public Cat busd(int i) { 7 //商品类型为什么就new什么 8 return new BMW(i); 9 } 10 11 @Override 12 public Fruit sendFruit() { 13 //商品类型为什么就new什么 14 return new Apple(); 15 } 16 }
1 package chouxiang; 2 3 public class ScheanB extends IsCat { 4 5 @Override 6 public Cat busd(int i) { 7 //商品类型为什么就new什么 8 return new Bennz(i); 9 } 10 11 @Override 12 public Fruit sendFruit() { 13 //商品类型为什么就new什么 14 return new Pearsr(); 15 } 16 }
建立客户端
1 package chouxiang; 2 3 public class Cilent { 4 public static void main(String[] args) { 5 6 IsCat iscat = new ScheanA(); 7 int num = 111; 8 iscat.busd(2); 9 if(num > 4){ 10 iscat.sendFruit().getColor(); 11 }else{ 12 System.out.println("你买的太少了,什么都不送"); 13 } 14 } 15 16 }