207
技術社區[雲棲]
Java麵向對象高級--實例分析—寵物商店
1、實例要求
實現一個寵物商店,在寵物商店中可以由多種寵物,(由用戶決定數量)。試表示出此種關係,並要求可以根據寵物的關鍵字查找到相應的寵物信息。所需要的寵物信息自行設計。
2、實例中主要使用到的知識點:接口、對象數組。
3、分析


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()) ;
}
}
}
};
最後更新:2017-04-03 15:22:09
上一篇:
網絡子係統24_橋接概念
下一篇:
11384 - Help is needed for Dexter 模擬 98
導入Excel出錯引出兩類異常——數據庫異常和業務異常處理方式
《計算機科學導論》一1.6計算機科學作為一門學科
政府安全資訊精選 2017年第十二期 中國網絡安全漏洞披露平均效率遠超美國; 美國美國國土安全部發布指令,要求聯邦機構強化Web和電子郵件安全
前端程序員必知的30個Chrome擴展
Struts2 中的設計模式
使命必達: 深入剖析WCF的可靠會話[編程篇](上)
從volatile解讀ConcurrentHashMap(jdk1.6.0)無鎖讀
Android 視圖切換效果
tomcat下的work目錄
【iPhone X重磅發布】攜A11人工智能芯片登場,人臉識別9大特征