JAVA學習-第四個代碼模型

來源:互聯網
上載者:User

標籤:

第四個代碼模型:介面應用

 

在現實生活之中經常會遇見如下的幾種情況:

 

                   · 在一片森林之中有多種樹木;

 

                   · 在商場之中有多種商品;

 

                   · 在一個停車場裡停放著多種車輛,例如:卡車、轎車、>機車、單車。

 

         下面類比以上的一個情境。現在有間超市,在超市之中提供有多種商品,現在要求實現商品的上架銷售和下架的功能,同時可以根據關鍵字查詢出商品的資訊。本程式只要求描述出類的結構即可。

 

 

 

 

範例:定義商品標準

 

interface Goods {         // 商品      

         public String getName() ;

         public double getPrice() ;

}

 

範例:定義超市類

 

class SuperMarket {     // 超市

         private Link goods ;      // 所有的商品

         public SuperMarket() {

                   this.goods = new Link() ;      // 準備出存放商品的空間

         }

         public void insert(Goods g) {         // 增加商品

                   this.goods.add(g) ;        // 儲存資料到鏈表

         }

         public void delete(Goods g) {         // 下架

                   this.goods.remove(g) ;  // 從鏈表刪除資料

         }

         public Link search(String keyWord) {

                   Link result = new Link() ;

                   Object obj [] = this.goods.toArray() ;

                   for (int x = 0 ; x < obj.length ; x ++) {

                            Goods gd = (Goods) obj[x] ;

                           if (gd.getName().contains(keyWord)) {    // 符合尋找條件

                                     result.add(gd) ;    // 儲存商品查詢結果

                            }

                   }

                   return result ;

         }

         public Link getGoods() {       // 取得本超市之中的所有商品

                   return this.goods ;

         }

}

 

範例:定義商品資料

 

class Cup implements Goods {

         private String name ;

         private double price ;

         public Cup(){}

         public Cup(String name,double price){

                   this.name = name ;

                   this.price = price ;

         }

         public String getName() {

                   return this.name ;

         }

         public double getPrice() {

                   return this.price ;

         }

         public boolean equals(Object obj) {

                   if (obj == null) {

                            return false ;

                   }

                   if (this == obj) {

                            return true ;

                   }

                   if (!(obj instanceof Cup)) {

                            return false ;

                   }

                   Cup cup = (Cup) obj ;

                   if (this.name.equals(cup.name) && this.price == cup.price) {

                            return true ;

                   }

                   return false ;

         }

         public String toString() {

                   return "【杯子】名稱 = " + this.name + ",價格:" + this.price ;

         }

}

class Computer implements Goods {

         private String name ;

         private double price ;

         public Computer(){}

         public Computer(String name,double price){

                   this.name = name ;

                   this.price = price ;

         }

         public String getName() {

                   return this.name ;

         }

         public double getPrice() {

                   return this.price ;

         }

         public boolean equals(Object obj) {

                   if (obj == null) {

                            return false ;

                   }

                   if (this == obj) {

                            return true ;

                   }

                   if (!(obj instanceof Computer)) {

                            return false ;

                   }

                   Computer com = (Computer) obj ;

                   if (this.name.equals(com.name) && this.price == com.price) {

                            return true ;

                   }

                   return false ;

         }

         public String toString() {

                   return "【電腦】名稱 = " + this.name + ",價格:" + this.price ;

         }

}

 

         此時電腦和杯子沒有本質的聯絡,但是他們都屬於商品。

 

public class TestDemo {

         public static void main(String args[]) {

                   SuperMarket market = new SuperMarket() ;

                   market.insert(new Cup("卡通杯",10.0)) ;

                   market.insert(new Cup("保溫杯",20.0)) ;

                   market.insert(new Cup("冷水杯",32.0)) ;

                   market.insert(new Computer("限量版卡通電腦",3200.0)) ;

                   market.insert(new Computer("毆打杯紀念電腦",99200.0)) ;

                   market.insert(new Computer("不怕水的電腦",39200.0)) ;

                   market.delete(new Cup("卡通杯",10.0)) ;

                   Object obj [] = market.search("卡通").toArray() ;

                   for (int x = 0 ; x < obj.length ; x ++) {

                            System.out.println(obj[x]) ;

                   }

         }

}

 

         本程式就是一個典型的面向介面的編程,不同類之間依靠介面進行串連。

 

JAVA學習-第四個代碼模型

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.