代码分析
Cat.java
1 package com.ftl.petshop; 2 3 class Cat implements Pet 4 { 5 private String name; 6 private String color; 7 private int age; 8 public Cat(String name, String color, int age) 9 { 10 this.name = name; 11 this.color = color; 12 this.age = age; 13 } 14 public String getName() 15 { 16 return name; 17 } 18 public void setName(String name) 19 { 20 this.name = name; 21 } 22 public String getColor() 23 { 24 return color; 25 } 26 public void setColor(String color) 27 { 28 this.color = color; 29 } 30 public int getAge() 31 { 32 return age; 33 } 34 public void setAge(int age) 35 { 36 this.age = age; 37 } 38 39 }
Dog.java
1 package com.ftl.petshop; 2 3 class Dog implements Pet 4 { 5 private String name; 6 private String color; 7 private int age; 8 public Dog(String name, String color, int age) 9 { 10 this.name = name; 11 this.color = color; 12 this.age = age; 13 } 14 public String getName() 15 { 16 return name; 17 } 18 public void setName(String name) 19 { 20 this.name = name; 21 } 22 public String getColor() 23 { 24 return color; 25 } 26 public void setColor(String color) 27 { 28 this.color = color; 29 } 30 public int getAge() 31 { 32 return age; 33 } 34 public void setAge(int age) 35 { 36 this.age = age; 37 } 38 39 }
Pet.java
1 package com.ftl.petshop; 2 3 interface Pet 4 { 5 public String getName(); 6 public String getColor(); 7 public int getAge(); 8 }
PetShop.java
1 package com.ftl.petshop; 2 3 4 class PetShop 5 { 6 private Pet pets[]; 7 private int foot; 8 public PetShop(int len) 9 { 10 if(len > 0) 11 { 12 this.pets = new Pet[len]; 13 } 14 else 15 { 16 this.pets = new Pet[1]; 17 } 18 } 19 20 public boolean add(Pet pet) 21 { 22 if (this.foot < this.pets.length) 23 { 24 this.pets[foot] = pet; 25 this.foot++; 26 return true; 27 } 28 else 29 { 30 return false; 31 } 32 } 33 34 public Pet[] search(String keyWord) 35 { 36 Pet[] p = null; 37 int count = 0; 38 for ( int i = 0; i <this.pets.length; i++) 39 { 40 if(this.pets[i]!=null) 41 { 42 if(this.pets[i].getName().indexOf(keyWord)!=-1 43 && this.pets[i].getColor().indexOf(keyWord)!=-1) 44 { 45 count++; 46 } 47 } 48 } 49 System.out.println("Sum " + count + "is Right..."); 50 p = new Pet[count]; 51 int f = 0; 52 for (int i = 0; i < this.pets.length;i++) 53 { 54 if(this.pets[i].getName().indexOf(keyWord)!=-1 55 && this.pets[i].getColor().indexOf(keyWord)!=-1) 56 { 57 p[f] = this.pets[i]; 58 f++; 59 } 60 } 61 62 return p; 63 } 64 65 66 };
PetShopDemo.java
1 package com.ftl.petshop; 2 3 public class PetShopDemo 4 { 5 6 public static void main(String[] args) 7 { 8 // TODO 自动生成的方法存根 9 PetShop ps = new PetShop(6); 10 ps.add(new Cat("W","W",2)); 11 ps.add(new Dog("W","W",2)); 12 ps.add(new Cat("B","B",2)); 13 ps.add(new Cat("B","W",2)); 14 ps.add(new Dog("W","BB",2)); 15 ps.add(new Cat("WW","W",2)); 16 ps.add(new Dog("AW","W",2)); 17 print(ps.search("W")); 18 } 19 public static void print(Pet p[]) 20 { 21 for (int i = 0; i < p.length; i++) 22 { 23 if(p[i]!=null) 24 { 25 System.out.println("Age "+ p[i].getAge() +" Name:"+ p[i].getColor() +" Color:" + p[i].getName()); 26 } 27 } 28 } 29 30 }
源码下载
原文地址:https://www.cnblogs.com/ftl1012/p/9351588.html
时间: 2024-10-13 19:00:58