〖面向对象(高级)〗_实例分析:宠物商店笔记

来源:互联网 发布:php if判断语句 编辑:程序博客网 时间:2024/06/04 18:50

〖面向对象(高级)〗_实例分析:宠物商店笔记

分类: Java
〖第6章:面向对象(高级)〗_实例分析:宠物商店笔记

实例要求:
实现一个宠物商店,在宠物商店中可以有多种(由用户决定数量)宠物,试表示出此种关系,并要求可以根据宠物的关键字查找到相应的宠物信息。所需要的宠物信息自行设计。

[java] view plaincopyprint?
  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 int setAge(){  
  22.         return this.age;  
  23.     }  
  24. }  
  25. class Dog implements Pet{  
  26.     private String name;  
  27.     private String color;  
  28.     private int age;  
  29.     public Dog(String name,String color,int age){  
  30.         this.setName(name);  
  31.         this.setColor(color);  
  32.         this.setAge(age);  
  33.     }  
  34.     public void setName(String name){  
  35.         this.name = name;  
  36.     }  
  37.     public void setColor(String color){  
  38.         this.color = color;  
  39.     }  
  40.     public int setAge(){  
  41.         return this.age;  
  42.     }  
  43. }  
  44. class PetShop{  
  45.     private Pet[] pets;  
  46.     private int foot;  
  47.     public PetShop(int len){  
  48.         if(len>0){  
  49.             this.pets = new Pet[len];  
  50.         }else{  
  51.             this.pets = new Pet[1];//至少开辟一个空间  
  52.         }  
  53.     }  
  54.     public boolean add(Pet pet){  
  55.         if(this.foot<this.pets.length){  
  56.             this.pets[this.foot] = pet;  
  57.             this.foot++;  
  58.             return true;  
  59.         }else{  
  60.             return false;  
  61.         }  
  62.     }  
  63.     public Pet[] search(String keyWord){  
  64.         Pet p[] = null;  
  65.         int count = 0;  
  66.         for(int i=0;i<this.pets.length;i++){  
  67.             if(this.pets[i]!=null){  
  68.                 if(this.pets[i].getName().indexOf(keyWord)!=-1||this.pets[i].getColor().indexOf(keyWord)!=-1){  
  69.                     count++;//修改查找到的记录数  
  70.                 }  
  71.             }  
  72.         }  
  73.         p = new Pet[count];//开辟指定的大小空间  
  74.         int f = 0;    //增加元素的位置标记  
  75.         for(int i=0;i<this.pets.length;i++){  
  76.             if(this.pets[i]!=null){  
  77.                 if(this.pets[i].getName().indexOf(keyWord)!=-1||this.pets[i].getColor().indexOf(keyWord)!=-1){  
  78.                     p[f] = this.pets[i];      
  79.                     count++;//修改查找到的记录数  
  80.                 }  
  81.             }  
  82.         }  
  83.         return p;  
  84.     }  
  85. }  
  86. public class PetShopDemo{  
  87.     public static void main(String args[]){  
  88.         PetShop ps = new PetShop(5);    //五个宠物  
  89.         ps.add(new Cat("白猫","白色的",2));    //增加宠物,成功  
  90.         ps.add(new Cat("黑猫","黑色的",3));    //增加宠物,成功  
  91.         ps.add(new Cat("花猫","花色的",3));    //增加宠物,成功  
  92.         ps.add(new Dog("拉不拉多","黄色的",3));    //增加宠物,成功  
  93.         ps.add(new Dog("金毛","金色的",2));    //增加宠物,成功  
  94.         ps.add(new Dog("啊黄","黄色的",2));    //增加宠物,成功  
  95.         print(ps.search("黑"));  
  96.       
  97.     }  
  98.     public static void print(Pet p[]){  
  99.         for(int i=0;i<p.length;i++){  
  100.             if(p[i]!=null){  
  101.                 System.out.println(p[i].getName()+","+p[i].getColor()+","+p[i].getAge());  
  102.             }  
  103.         }  
  104.     }  
  105.   
  106. }  



5.总结:

    在本程序中实际上最重要的就是接口的设计,只要接口设计的足够合理,则程序开发会有很高的灵活性,使用接口可以进行解耦合操作。

    定义及声明数组时可以使用int[] num = null或 int nul[] = null;两种形式皆可!

0 0
原创粉丝点击