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 PetShopDemo{
public static void main(String args[]){
PetShop ps = new PetShop(5) ; // 五个宠物
ps.add(new Cat("白猫","白色的",2)) ; // 增加宠物,成功
ps.add(new Cat("黑猫","黑色的",3)) ; // 增加宠物,成功
ps.add(new Cat("花猫","花色的",3)) ; // 增加宠物,成功
ps.add(new Dog("拉步拉多","黄色的",3)) ; // 增加宠物,成功
ps.add(new Dog("金毛","金色的",2)) ; // 增加宠物,成功
ps.add(new Dog("黄狗","黑色的",2)) ; // 增加宠物,失败
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()) ;
}
}
}
};

吴裕雄--天生自然JAVA面向对象高级编程学习笔记:宠物商店实例分析的更多相关文章

  1. 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:继承的应用

    class Array{ // 表示数组 private int temp[] ; // 整型数组 private int foot ; // 定义添加位置 public Array(int len) ...

  2. 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:匿名内部类

    interface A{ public void printInfo() ; // } class B implements A{ // 实现接口 public void printInfo(){ S ...

  3. 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:包装类

    public class WrapperDemo01{ public static void main(String args[]){ int x = 30 ; // 基本数据类型 Integer i ...

  4. 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:Object类

    class Demo{ // 定义Demo类,实际上就是继承了Object类 }; public class ObjectDemo01{ public static void main(String ...

  5. 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:抽象类与接口的应用

    abstract class A{ // 定义抽象类A public abstract void print() ; // 定义抽象方法print() }; class B extends A { / ...

  6. 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:instanceof关键字

    class A{ // 定义类A public void fun1(){ // 定义fun1()方法 System.out.println("A --> public void fun ...

  7. 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:对象的多态性

    class A{ // 定义类A public void fun1(){ // 定义fun1()方法 System.out.println("A --> public void fun ...

  8. 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:接口的基本实现

    interface A{ // 定义接口A public static final String AUTHOR = "李兴华" ; // 全局常量 public abstract ...

  9. 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:final关键字

    final class A{ // 使用final定义类,不能有子类 }; class B extends A{ // 错误,不能被继承 }; class A{ public final void p ...

随机推荐

  1. LeetCode 234. Palindrome Linked List(判断是否为回文链表)

    题意:判断是否为回文链表,要求时间复杂度O(n),空间复杂度O(1). 分析: (1)利用快慢指针找到链表的中心 (2)进行步骤(1)的过程中,对前半部分链表进行反转 (3)如果链表长是偶数,首先比较 ...

  2. Tomcat热部署与热加载!

    所谓的热部署与热加载就是两个值:(reloadable='true'与autoDeloy='true')

  3. 堆(c++)

    5分钟速成堆 FBI⚠WARNING 本文要素过多 吐槽 堆是我迄今为止学过最简单的数据结构 我还没学会最小生成树.最短路时就学会了 堆实用高效,值得推荐 (如果你看完了这篇文章还不会,你可以直接Co ...

  4. 类似discuz密码的生成规则

    /* 生成一个串,uniqid(rand()): uniqid(prefix,more_entropy) 函数基于以微秒计的当前时间,生成一个唯一的 ID. 如果 prefix 参数为空,则返回的字符 ...

  5. 通过view获取所在的viewController对象

    建议写成UIView的分类,如下: .h - (UIViewController *)viewController; .m - (UIViewController *)viewController { ...

  6. 第六周之Hadoop学习(六)

    继续上周开启telnet的过程,这个过程发现win10上运行不了telnet的命令 原因大概在于没有开启telnet服务,从网上下载好telent服务端,安装后继续尝试是否能在win10上使用hado ...

  7. Lesson 14 The Butterfly Effect

    Why do small errors make it impossible to predict the weather system with a high degree of accuracy? ...

  8. Django中的prefetch_related()函数优化

    对于多对多字段(ManyToManyField)和一对多字段, 可以使用prefetch_related()来进行优化 prefetch_related()和select_related()的设计目的 ...

  9. 使用gcc编译c语言解码ascii码

    vi test.c 输入代码: #include<stdio.h> int main(void) { char *p = (char *)"\xE6\x8A\xB1\xE6\xA ...

  10. 02-09Android学习进度报告九

    今天我学习了关于Adapter的基础知识,了解了Android开发的一些思路和架构. 首先我了解了Adapter的概念以及开发过程中常用的Adapter: BaseAdapter:抽象类,实际开发中我 ...