日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > 互聯網 > 接口設計的一個程序實例(寵物商店)

接口設計的一個程序實例(寵物商店)

來源:程序員人生   發布時間:2014-09-17 09:33:54 閱讀次數:2754次

    外延     

                   以往寫博客沒有用那種目錄的結構,今天終于會用了。如何使用目錄結構請參照:目錄結構的使用

    實例要求

                實現一個寵物商店,寵物商店的寵物可以有多種(數量由用戶決定),試表示此種關系,并要求根據寵物的的關鍵字查找寵物的信息,所需要的寵物信息自行設計。

    所用知識

              接口,對象數組

    實例分析

             寵物信息可以自行設計,我們可以簡單設計三個屬性:名字、顏色、年齡
            寵物的類別很多、所以寵物應該是一個標準(接口)
            寵物商店有多種寵物,應該是一個寵物的對象數組  

    代碼實現

          
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 ++ ; //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)) ; // 增加寵物,失敗 數組長度為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()) ; } } } };

       總結

         本程序最主要的是接口的設計,    注意查詢時indexof的使用,程序設計要嚴謹(一些判斷的使用)。
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 91精品国产色综合久久不卡98口 | 毛片在线免费播放 | 日韩在线免费电影 | 九九热免费精品视频 | 久久九九 | 欧美综合久久 | 日韩精品高清一区二区三区 | 蜜桃精品久久久久久久免费影院 | 亚洲国产视频一区 | 成人国产精品久久久 | 成人激情视频在线 | 亚洲v天堂 | 就要操 | 日韩成人在线播放 | 欧美在线一区二区 | 午夜视频在线观看网站 | 色网在线播放 | 亚洲精品电影在线 | 久久都是精品 | 久久精品视频偷拍 | 亚洲福利一区二区 | 国产一区二区三区精品久久久 | 国产iv一区二区三区 | 免费精品| 欧美日韩亚洲国产 | 国产精品欧美在线 | 爱情岛论坛首页网址 | 欧美成人精品一区二区 | 日韩a电影 | 国产视频二区 | 免费激情网址 | 亚洲欧美日本在线 | 欧美国产日本在线观看 | 国产精品久久久av | 日韩国产 | 91精品国产影片一区二区三区 | 五月综合激情网 | 福利视频一二三在线视频免费观看 | 亚洲一区二区三区久久 | 最新一级毛片 | 91精品国产91久久久久 |