吴裕雄--天生自然JAVA面向对象高级编程学习笔记:宠物商店实例分析
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面向对象高级编程学习笔记:宠物商店实例分析的更多相关文章
- 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:继承的应用
class Array{ // 表示数组 private int temp[] ; // 整型数组 private int foot ; // 定义添加位置 public Array(int len) ...
- 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:匿名内部类
interface A{ public void printInfo() ; // } class B implements A{ // 实现接口 public void printInfo(){ S ...
- 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:包装类
public class WrapperDemo01{ public static void main(String args[]){ int x = 30 ; // 基本数据类型 Integer i ...
- 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:Object类
class Demo{ // 定义Demo类,实际上就是继承了Object类 }; public class ObjectDemo01{ public static void main(String ...
- 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:抽象类与接口的应用
abstract class A{ // 定义抽象类A public abstract void print() ; // 定义抽象方法print() }; class B extends A { / ...
- 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:instanceof关键字
class A{ // 定义类A public void fun1(){ // 定义fun1()方法 System.out.println("A --> public void fun ...
- 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:对象的多态性
class A{ // 定义类A public void fun1(){ // 定义fun1()方法 System.out.println("A --> public void fun ...
- 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:接口的基本实现
interface A{ // 定义接口A public static final String AUTHOR = "李兴华" ; // 全局常量 public abstract ...
- 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:final关键字
final class A{ // 使用final定义类,不能有子类 }; class B extends A{ // 错误,不能被继承 }; class A{ public final void p ...
随机推荐
- spark wordcount程序
spark wordcount程序 IllegalAccessError错误 这个错误是权限错误,错误的引用方法,比如方法中调用private,protect方法. 当然大家知道wordcount业务 ...
- text-align:justify 失效问题。
text-align:justify 失效问题. <div class="fmlist_left"> <p> <span> 品名 <i c ...
- php cli 下 php.ini 配置
// 查看phpcli 模式下 扩展 php -m // 查看php cli 版本 php -v 查看命令行的ini路径,命令行下运行 php --ini Loaded Configuration F ...
- mac 终端连接服务器报错
今天在连接虚拟机服务器时突然报了一个 WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!的错误.  会出现这个错误的原因是在第一次进行SSH连接时,会生 ...
- ios中为视图添加圆角
1.使用 layer设置指定圆角 或者设定一个或几个圆角 码修 关注 2017.04.20 19:03* 字数 107 阅读 656评论 0喜欢 0 由于项目中需要给按钮左下 和左上加圆角,我司可爱的 ...
- BUUCTF知识记录
[强网杯 2019]随便注 先尝试普通的注入 发现注入成功了,接下来走流程的时候碰到了问题 发现过滤了select和where这个两个最重要的查询语句,不过其他的过滤很奇怪,为什么要过滤update, ...
- git 修改分支 删除分支 新增分支
一.修改分支名 1.本地分支重命名 git branch -m oldName newName 2.将重命名后的分支推送到远程 git push origin newName 3.重新更新所有分支 g ...
- ZOJ4102 Array in the Pocket(2019浙江省赛)
贪心~ #include<bits/stdc++.h> using namespace std; ; int a[maxn]; int b[maxn]; int vis[maxn]; se ...
- Struts2报错异常Method "setUser" failed for object com.mikey.action.ConverterAction@dd34285
在写类型转换的时候发现报错 异常信息 ognl.MethodFailedException: Method "setUser" failed for object com.mike ...
- CentOS7虚拟机优化
CentOS7: 将网卡配置重点关注的地方为: [root@master ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33 BOOTPROTO=st ...