Java面向对象编程(宠物商店)

宠物商店

现在要求建立宠物商店,里面就有进行要销售宠物的上架,下架,关键字查询。

要求:描述出程序的关系即可

那么假设对于宠物的信息只要求有三项:名字,年龄,颜色  

那么此时对于的关系:一个宠物商店会有多种宠物。如果按照关系表来讲这是一对多的关系映射,但是现在的问题是,一方是宠物商店,而多方应该是宠物,但是宠物又分为猫,狗,猪,鱼 等等。 

1.建立宠物标准

1 interface Pet{//定义宠物
2     public String getName();
3     public String getColor();
4     public int getAge();
5 }

2.对于宠物商店,只关注于宠物的标准,而不关心那种宠物

 1 class PetShop{
 2     private Link pets=new Link();    //开辟一个链表,保存多个宠物
 3     public void add(Pet pet){//上架上的宠物,
 4         this.pets.add(pet);
 5     }
 6     public void delete(Pet pet){
 7         this.pets.remove(pet);
 8     }
 9     public Link getPets(){//取得全部宠物
10         return this.pets;
11     }
12     public Link search(String keyWord){
13         Link result=new Link();    //保存查询结果
14         Object[] data =this.pets.toArray();
15         for(int x=0;x<data.length;x++){
16             Pet pet =(Pet)data[x];
17             if(pet.getName().contains(keyWord)||pet.getColor().contain(keyWord)){
18                 result.add(pet);//满足查询结果
19             }
20         }
21         return result;
22     }
23 }

3.定义宠物狗:

 1 class Dog implements Pet{
 2     private String name;
 3     private int age;
 4     private String color;
 5     public Dog(String name,int age,String color){
 6         this.name=name;
 7         this.age=age;
 8         this.color=color;
 9     }
10     public String getName(){
11         return this.name;
12     }
13     public int getAge(){
14         return this.age;
15     }
16     public String getColor(){
17         return this.color;
18     }
19     public boolean equals(Object obj){
20         if(obj==null){
21             return false;
22         }
23         if(this==obj){
24             return true;
25         }
26         if(!(obj instanceof Dog)){
27             return false;
28         }
29         Dog pet=(Dog)obj;
30         return this.name.equals(pet.name)&&this.age==pet.age&&this.color.equals(pet.color);
31         
32     }
33     public String toString(){
34         return "【狗】名字:"+this.name+",年龄:"+this.age+",颜色:"+this.color;
35     }
36 }

4.定义宠物猫

 1 class Cat implements Pet{
 2     private String name;
 3     private int age;
 4     private String color;
 5     public Cat(String name,int age,String color){
 6         this.name=name;
 7         this.age=age;
 8         this.color=color;
 9     }
10     public String getName(){
11         return this.name;
12     }
13     public int getAge(){
14         return this.age;
15     }
16     public String getColor(){
17         return this.color;
18     }
19     public boolean equals(Object obj){
20         if(obj==null){
21             return false;
22         }
23         if(this==obj){
24             return true;
25         }
26         if(!(obj instanceof Cat)){
27             return false;
28         }
29         Cat pet=(Cat)obj;
30         return this.name.equals(pet.name)&&this.age==pet.age&&this.color.equals(pet.color);
31         
32     }
33     public String toString(){
34         return "【猫】名字:"+this.name+",年龄:"+this.age+",颜色:"+this.color;
35     }
36 }

5.编写测试程序

 1 public class Newbegin{
 2     public static void main(String args[]) throws Exception{
 3         PetShop ps=new PetShop();
 4         ps.add(new Dog("黑狗",1,"黑色"));
 5         ps.add(new Dog("金毛",2,"黄色"));
 6         ps.add(new Dog("腊肠",1,"金色"));
 7         ps.add(new Dog("拉布拉多",1,"金色"));
 8         ps.add(new Cat("加菲猫",1,"金色"));
 9         ps.add(new Cat("波斯猫",1,"白色"));
10         ps.delete(new Dog("腊肠",1,"金色"));
11         Link all=ps.search("金");
12         Object[] data=all.toArray();
13         for(int x=0;x<data.length;x++){
14             System.out.println(data[x]);
15         
16         }
17     }  
18 }       

实际之中这种形式的代码在生活中处处可见

  一个公园可以有多种植物

  动物园可以有多种动物;

  一个衣柜可以有多件衣服;

  一个人可以吃多种食物;

总结:

  在以后的代码开发过程之中一切都要以接口设计为主。

  

猜你喜欢

转载自www.cnblogs.com/Tony98/p/10451479.html