package bjl.petshop;
public interface Pet {
public String getName();
public String getColor(); //接口的抽象方法
}
package bjl.petshop;
public class Cat implements Pet{
private String name;
private String color;
public Cat(){
}
public Cat(String name,String color){
this.setName(name);
this.setColor(color);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
package bjl.petshop;
public class PetShop {
private Pet[] pets;
private int foot;
public int len;
public PetShop(int len){
if(len>0){
this.pets = new Pet[len]; //数组名=new 数据类型[长度],分配内存给数组
}
else{
this.pets = new Pet[1];
}
}
public boolean AddPet(Pet pet){
if(this.foot < this.pets.length){
this.pets[this.foot] = pet;
this.foot++;
return true;
}
else{
return false;
}
}
public boolean SearchPet(String keyWord){
boolean count = false;
for(int i=0;i<=this.pets.length;i++){
if(this.pets[i].getName().equals(keyWord)){
count = true;
}
else{
count = false;
}
}
return count;
}
}
package bjl.petshop;
public class Test {
public static void main(String args[]){
PetShop ps = new PetShop(6);
ps.AddPet(new Cat("小白","白色"));
ps.AddPet(new Cat("小黑","黑色"));
ps.AddPet(new Cat("小花","花色"));
ps.AddPet(new Cat("小红","红色"));
ps.AddPet(new Cat("小黄","黄色"));
System.out.println(ps.SearchPet("小白"));
}
}