实例分析:宠物商店
1 interface Pet{ // 定义宠物接口 2 public String getName() ; 3 public String getColor() ; 4 public int getAge() ; 5 } 6 class Cat implements Pet{ // 猫是宠物,实现接口 7 private String name ; // 宠物名字 8 private String color ; // 宠物颜色 9 private int age ; // 宠物年龄 10 public Cat(String name,String color,int age){ 11 this.setName(name) ; 12 this.setColor(color) ; 13 this.setAge(age) ; 14 } 15 public void setName(String name){ 16 this.name = name ; 17 } 18 public void setColor(String color){ 19 this.color = color; 20 } 21 public void setAge(int age){ 22 this.age = age ; 23 } 24 public String getName(){ 25 return this.name ; 26 } 27 public String getColor(){ 28 return this.color ; 29 } 30 public int getAge(){ 31 return this.age ; 32 } 33 }; 34 class Dog implements Pet{ // 狗是宠物,实现接口 35 private String name ; // 宠物名字 36 private String color ; // 宠物颜色 37 private int age ; // 宠物年龄 38 public Dog(String name,String color,int age){ 39 this.setName(name) ; 40 this.setColor(color) ; 41 this.setAge(age) ; 42 } 43 public void setName(String name){ 44 this.name = name ; 45 } 46 public void setColor(String color){ 47 this.color = color; 48 } 49 public void setAge(int age){ 50 this.age = age ; 51 } 52 public String getName(){ 53 return this.name ; 54 } 55 public String getColor(){ 56 return this.color ; 57 } 58 public int getAge(){ 59 return this.age ; 60 } 61 }; 62 class PetShop{ // 宠物商店 63 private Pet[] pets ; // 保存一组宠物 64 private int foot ; 65 public PetShop(int len){ 66 if(len>0){ 67 this.pets = new Pet[len] ; // 开辟数组大小 68 }else{ 69 this.pets = new Pet[1] ; // 至少开辟一个空间 70 } 71 } 72 public boolean add(Pet pet){ // 增加的是一个宠物 73 if(this.foot<this.pets.length){ 74 this.pets[this.foot] = pet ; // 增加宠物 75 this.foot ++ ; 76 return true ; 77 }else{ 78 return false ; 79 } 80 } 81 public Pet[] search(String keyWord){ 82 // 应该确定有多少个宠物符合要求 83 Pet p[] = null ; 84 int count = 0 ; // 记录下会有多少个宠物符合查询结果 85 for(int i=0;i<this.pets.length;i++){ 86 if(this.pets[i]!=null){ // 表示此位置有宠物 87 if(this.pets[i].getName().indexOf(keyWord)!=-1 88 ||this.pets[i].getColor().indexOf(keyWord)!=-1){ 89 count++ ; // 修改查找到的记录数 90 } 91 } 92 } 93 p = new Pet[count] ; // 开辟指定的大小空间 94 int f = 0 ; // 增加元素的位置标记 95 for(int i=0;i<this.pets.length;i++){ 96 if(this.pets[i]!=null){ // 表示此位置有宠物 97 if(this.pets[i].getName().indexOf(keyWord)!=-1 98 ||this.pets[i].getColor().indexOf(keyWord)!=-1){ 99 p[f] = this.pets[i] ; 100 f++ ; 101 } 102 } 103 } 104 return p ; 105 106 } 107 }; 108 public class PetShopDemo{ 109 public static void main(String args[]){ 110 PetShop ps = new PetShop(5) ; // 五个宠物 111 ps.add(new Cat("白猫","白色的",2)) ; // 增加宠物,成功 112 ps.add(new Cat("黑猫","黑色的",3)) ; // 增加宠物,成功 113 ps.add(new Cat("花猫","花色的",3)) ; // 增加宠物,成功 114 ps.add(new Dog("拉步拉多","黄色的",3)) ; // 增加宠物,成功 115 ps.add(new Dog("金毛","金色的",2)) ; // 增加宠物,成功 116 ps.add(new Dog("黄狗","黑色的",2)) ; // 增加宠物,失败 117 print(ps.search("黑")) ; 118 } 119 public static void print(Pet p[]){ 120 for(int i=0;i<p.length;i++){ 121 if(p[i]!=null){ 122 System.out.println(p[i].getName() + "," + p[i].getColor() 123 +"," + p[i].getAge()) ; 124 } 125 } 126 } 127 };
View Code
总结:在本程序中最重要的就是接口的设计,只要接口设计的足够合理,则程序开发就会有很高的灵活性,使用接口可以进行解耦合操作。
版权声明:本文为centvinzz原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。