介面設計的一個程式執行個體(寵物商店),執行個體商店

來源:互聯網
上載者:User

介面設計的一個程式執行個體(寵物商店),執行個體商店
    外延                        以往寫部落格沒有用那種目錄的結構,今天終於會用了。如何使用目錄結構請參照:目錄結構的使用    執行個體要求                實現一個寵物商店,寵物商店的寵物可以有多種(數量由使用者決定),試表示此種關係,並要求根據寵物的的關鍵字尋找寵物的資訊,所需要的寵物資訊自行設計。    所用知識              介面,對象數組    執行個體分析             寵物資訊可以自行設計,我們可以簡單設計三個屬性:名字、顏色、年齡            寵物的類別很多、所以寵物應該是一個標準(介面)            寵物商店有多種寵物,應該是一個寵物的對象數組      代碼實現          

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)) ;// 增加寵物,失敗    數組長度為5print(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的使用,程式設計要嚴謹(一些判斷的使用)。
教一個基礎的JAVA程式題,幫忙解惑

以學習之名 我寫了個東西 值得參考的話 你看看。感覺不行的話 就當學習了

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//主類
public class BaiduDemo6 {
public static void main(String[] a) {
Pet[] mypet=new Pet []{
new Cat("小貓","白色",2,100f),
new Dog("小g","白色",3,100f),
new Cat("小g","白色",3,100f),
new Dog("小i","灰色",4,200f),
new Dog("小s","白黑色",3,100f),
new Cat("小c","黑色",3,200f)
};
//非客戶指定數量方式
PetShop petshop1=new PetShop(mypet);
petshop1.add(new Cat("小cc","黑灰色",3,200f));
System.out.println("寵物店當前寵物:\n"+petshop1);
//尋找 查詢關係字'g'
System.out.println("尋找結果"+petshop1.search("g"));
//查詢關係字'白色'
System.out.println("尋找結果"+petshop1.search("白色"));

//客戶指定數量方式
PetShop petshop2=new PetShop(3);
System.out.println("寵物店當前寵物:\n"+petshop2);
//增加
petshop2.add(new Cat("小貓","白色",2,100f));
petshop2.add(new Dog("小g","白色",3,100f));
petshop2.add(new Cat("小g","白色",3,100f));
petshop2.add(new Cat("小c","黑色",3,200f));
System.out.println("寵物店當前寵物:\n"+petshop2);

//尋找 查詢關係字'g'
Syste......餘下全文>>
 
一個java代碼詳細設計的例子,以介面為主的

//定義介面IFruit
public interface IFruit{
public String name="fruit";
public String getName(); //介面中的抽象方法
}
//定義Farmer類用建立不同的執行個體對象
public class Farmer{
public void plant(IFruit fruit){
System.out.println("種植了:"+fruit.getName());
}
}
//Apple是實現IFruit介面
public class Apple implements IFruit{
public String getName(){
return "apple";
}
}
//Orange類實現IFruit介面
public class Orange implements IFruit{
public String getName(){
return "orange";
}
}

//測試類別
class Demo{
public static void main(String[] args){
Farmer farmer=new Farmer();
farmer.plant(new Apple());
farmer.plant(new Orange());
}
}

//注意:非靜態類實現介面時必須實現相應的抽象方法
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.