接口設(shè)計(jì)的一個(gè)程序?qū)嵗▽櫸锷痰辏?/h1>
來源:程序員人生 發(fā)布時(shí)間:2014-09-17 09:33:54 閱讀次數(shù):2750次
外延
以往寫博客沒有用那種目錄的結(jié)構(gòu),今天終于會(huì)用了。如何使用目錄結(jié)構(gòu)請(qǐng)參照:目錄結(jié)構(gòu)的使用
實(shí)例要求
實(shí)現(xiàn)一個(gè)寵物商店,寵物商店的寵物可以有多種(數(shù)量由用戶決定),試表示此種關(guān)系,并要求根據(jù)寵物的的關(guān)鍵字查找寵物的信息,所需要的寵物信息自行設(shè)計(jì)。
所用知識(shí)
接口,對(duì)象數(shù)組
實(shí)例分析
寵物信息可以自行設(shè)計(jì),我們可以簡(jiǎn)單設(shè)計(jì)三個(gè)屬性:名字、顏色、年齡
寵物的類別很多、所以寵物應(yīng)該是一個(gè)標(biāo)準(zhǔn)(接口)
寵物商店有多種寵物,應(yīng)該是一個(gè)寵物的對(duì)象數(shù)組
代碼實(shí)現(xiàn)
interface Pet{ // 定義寵物接口
public String getName() ;
public String getColor() ;
public int getAge() ;
}
class Cat implements Pet{ // 貓是寵物,實(shí)現(xiàn)接口
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{ // 狗是寵物,實(shí)現(xiàn)接口
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] ; // 開辟數(shù)組大小
}else{
this.pets = new Pet[1] ; // 至少開辟一個(gè)空間
}
}
public boolean add(Pet pet){ // 增加的是一個(gè)寵物
if(this.foot<this.pets.length){
this.pets[this.foot] = pet ; // 增加寵物
this.foot ++ ; //foot 數(shù)組下標(biāo)
return true ;
}else{
return false ;
}
}
public Pet[] search(String keyWord){
// 應(yīng)該確定有多少個(gè)寵物符合要求
Pet p[] = null ;
int count = 0 ; // 記錄下會(huì)有多少個(gè)寵物符合查詢結(jié)果
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++ ; // 修改查找到的記錄數(shù)
}
}
}
p = new Pet[count] ; // 開辟指定的大小空間
int f = 0 ; // 增加元素的位置標(biāo)記
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) ; // 五個(gè)寵物
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)) ; // 增加寵物,失敗 數(shù)組長(zhǎng)度為5
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()) ;
}
}
}
};
總結(jié)
本程序最主要的是接口的設(shè)計(jì), 注意查詢時(shí)indexof的使用,程序設(shè)計(jì)要嚴(yán)謹(jǐn)(一些判斷的使用)。
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)