工厂模式 -手机

播放接口

package HandSet;
/**
 * 播放
 * @author Administrator
 *
 */
public interface Play {
    /**
     * 播放
     */
    public void play();
}

上网接口

package HandSet;
/**
 *     上网
 * @author Administrator
 *
 */
public interface NetWork {
    /**
     * 上网
     */
    public void Network();
}

拍照接口

package HandSet;
/**
 *     照相
 * @author Administrator
 *
 */
public interface Photo {
    /**
     * 照相
     */
    public void photo();
}

手机抽象类

package HandSet;
/**
 * 手机类 
 * @author Administrator
 *
 */
public abstract class HandSet {
    private String brand;    //品牌
    private String type;    //型号
    
    
    /**
     * 构造方法
     */
    public HandSet() {}
    public HandSet(String brand,String type) {
        this.brand=brand;
        this.type=type;
    }
    /**
     * setter getter
     * @return
     */
    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    /**
     * 发送短信
     */
    public abstract void sendInfo();
    /**
     * 打电话
     */
    public abstract void call();
    /**
     * 手机信息
     */
    public void info() {
        System.out.println("这是一款型号为"+brand+"的"+type+"手机");
    }
}

普通手机

package HandSet;
/**
 * 普通手机
 * @author Administrator
 *
 */
public class CommonHandset extends HandSet implements Play {
    /**
     * 构造方法
     */
    public CommonHandset() {}
    public CommonHandset(String brand,String type) {
        super(brand,type);
    }
    
    /**
     * 打电话
     */
    public void call() {
        System.out.println("开始语音通话。。。。。。");
    }
    
    /**
     * 发送短信
     */
    public void sendInfo() {
        System.out.println("发送文字信息。。。。。。");
    }
    
    /**
     *    播放
     */
    public void play() {
        System.out.println("开始播放音乐《金莎》- 笨蛋......");
    }


}

智能手机

package HandSet;
/**
 * 智能手机
 * @author Administrator
 *
 */
public class IntelligenceHandset extends HandSet implements Play,NetWork,Photo{
    /**
     * 构造方法
     */
    public IntelligenceHandset() {}
    public IntelligenceHandset(String brand,String type) {
        super(brand,type);
    }
    /**
     * 打电话
     */
    public void call() {
        System.out.println("开始视频通话。。。。。。");
    }
    
    /**
     * 发送短信
     */
    public void sendInfo() {
        System.out.println("发送图片带有文字的信息。。。。。。");
    }
    
    /**
     *    播放
     */
    public void play() {
        System.out.println("开始播放视频《权利的游戏》......");
    }
    /**
     * 拍照
     */
    public void photo() {
        System.out.println("咔嚓。。。。。。拍照成功");
        
    }
    /**
     * 上网
     */
    public void Network() {
        System.out.println("已经启用移动网络......");
        
    }
}

 

手机测试类

package HandSet;
/**
 * 手机测试类
 * @author Administrator
 *
 */
public class HandsetTest {
    public static void main(String[] args) {
        /*
         * 创建普通手机
         */
        HandSet commonHandset=new CommonHandset("索尼爱立信","G502C");        //父类引用指向子类类型
        CommonHandset common=(CommonHandset)commonHandset;                //强转成子类
        common.info();
        common.play();
        common.sendInfo();
        common.call();
        
        System.out.println();
        /*
         * 创建智能手机
         */
        HandSet intelligenceHandset=new IntelligenceHandset("HTC","I9100");
        IntelligenceHandset zhi=(IntelligenceHandset)intelligenceHandset;
        zhi.info();
        zhi.Network();
        zhi.play();
        zhi.photo();
        zhi.sendInfo();
        zhi.call();
    }
}

 

测试

 

下载

工厂模式 -手机

链接:https://pan.baidu.com/s/1gmzJBYe05lfjriI765f_tQ
提取码:fuoa

posted @ 2019-02-18 16:01  纸灰  阅读(237)  评论(0编辑  收藏  举报