码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA学习-第四个代码模型

时间:2015-07-19 23:30:05      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:

第四个代码模型:接口应用

 

在现实生活之中经常会遇见如下的几种情况:

 

                   · 在一片森林之中有多种树木;

 

                   · 在商场之中有多种商品;

 

                   · 在一个停车场里停放着多种车辆,例如:卡车、轿车、摩托车、自行车。

 

         下面模拟以上的一个场景。现在有间超市,在超市之中提供有多种商品,现在要求实现商品的上架销售和下架的功能,同时可以根据关键字查询出商品的信息。本程序只要求描述出类的结构即可。

 

技术分享

 

 

 

范例:定义商品标准

 

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学习-第四个代码模型

标签:

原文地址:http://www.cnblogs.com/kvikon/p/4659933.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!