interface Pet{
public String getName() ;
public String getColor() ;
public int getAge() ;
}
class Cat implements Pet{
private String name ;
private String color ;
private int age ;
public Cat(String name,String color,int age){
this.setName(name) ;
this.setColor(color) ;
this.setAge(age) ;
}
public void setName(String name){
this.name = name ;
}
public void setColor(String color){
this.color = color;
}
public void setAge(int age){
this.age = age ;
}
public String getName(){
return this.name ;
}
public String getColor(){
return this.color ;
}
public int getAge(){
return this.age ;
}
};
class Dog implements Pet{
private String name ;
private String color ;
private int age ;
public Dog(String name,String color,int age){
this.setName(name) ;
this.setColor(color) ;
this.setAge(age) ;
}
public void setName(String name){
this.name = name ;
}
public void setColor(String color){
this.color = color;
}
public void setAge(int age){
this.age = age ;
}
public String getName(){
return this.name ;
}
public String getColor(){
return this.color ;
}
public int getAge(){
return this.age ;
}
};
class PetShop{
private Pet[] pets ;
private int foot ;
public PetShop(int len){
if(len>0){
this.pets = new Pet[len] ;
}else{
this.pets = new Pet[1] ;
}
}
public boolean add(Pet pet){
if(this.foot<this.pets.length){
this.pets[this.foot] = pet ;
this.foot ++ ;
return true ;
}else{
return false ;
}
}
public Pet[] search(String keyWord){
Pet p[] = null ;
int count = 0 ;
for(int i=0;i<this.pets.length;i++){
if(this.pets[i]!=null){
if(this.pets[i].getName().indexOf(keyWord)!=-1
||this.pets[i].getColor().indexOf(keyWord)!=-1){
count++ ;
}
}
}
p = new Pet[count] ;
int f = 0 ;
for(int i=0;i<this.pets.length;i++){
if(this.pets[i]!=null){
if(this.pets[i].getName().indexOf(keyWord)!=-1
||this.pets[i].getColor().indexOf(keyWord)!=-1){
p[f] = this.pets[i] ;
f++ ;
}
}
}
return p ; }
};
public class HomeworkDemo{
public static void main(String args[]){
PetShop ps = new PetShop(5) ;
ps.add(new Cat("英短","灰色的",3)) ;
ps.add(new Cat("布偶猫","白色的",2)) ;
ps.add(new Cat("花猫","花色的",4)) ;
ps.add(new Dog("哈士奇","黑白色的",5)) ;
ps.add(new Dog("金毛","金色的",6)) ;
ps.add(new Dog("黑贝","棕色的",3)) ;
print(ps.search("白")) ;
}
public static void print(Pet p[]){
for(int i=0;i<p.length;i++){
if(p[i]!=null){
System.out.println(p[i].getName() + "," + p[i].getColor()
+"," + p[i].getAge()) ;
}
}
}
};

最新文章

  1. .net 实体类与json转换(.net自带类库实现)
  2. thinkphp安装 版本 3.1.3
  3. HBase之集群状态
  4. mysql学习笔记 第七天
  5. C中scanf/gets/fgets的区别
  6. Android 的 init.rc 文件简介【转】
  7. Linux_系统信息
  8. C++:对象的赋值和复制
  9. Nginx+FastCGI+C
  10. 多线程同步循环打印和Guarded suspension 模式
  11. uml系列(八)——部署图与构件图
  12. 如何用Python写一个计算器软件 附带效果图
  13. 最优化算法:BFGS算法全称和L-BFGS算法全称
  14. 跟我学ASP.NET MVC之一:开篇有益
  15. [DQN] What is Deep Reinforcement Learning
  16. 搭建ReactNative时的最普遍的错误—— &quot;:CFBundleIdentifier&quot;, Does Not Exist
  17. 自己从0开始学习Unity的笔记 II (C#条件语句基础练习)
  18. Codeforces 950.E Data Center Maintenance
  19. react组件生命
  20. 初试Docker on Debian on VirtualBox

热门文章

  1. id、class等各种选择器总结
  2. 开启text汇聚排序
  3. Django Form表单组件
  4. dnsmasq 设置自动获取主机名
  5. 清北学堂北京大学冯哲神仙讲课day2
  6. 查看mysql中sql语句执行时间
  7. hyperledger fabric 架设命令
  8. SAP 成套销售&amp;按项目销售
  9. canvas连线特效
  10. React-native完整配置流程