用继承的方式实现一个打印机,父类(打印机),子类(黑白打印机、彩色打印机)

package com.Summer_0426.cn;

/**
 * @author Summer
 * 用继承实现打印机
 * 需求:
 * 定义一个父类打印机:Printer
 * 父类属性:打印的内容
 * 父类方法:打印()
 * 定义子类1:黑白打印机BlackPrinter
 * 重写父类打印方法(黑白打印内容)
 * 定义子类2:
 * 重写父类打印方法(彩色打印内容)
 */
class Printer{
    private String str;
    public Printer(String str){
        this.str = str;
    }
    public String getStr() {
        return str;
    }
    public void print(){
        System.out.println("打印"+str);
    }    
}

class ColorPrinter extends Printer{
    public ColorPrinter(String str){
        super(str);
    }
    public void print(){
        System.out.println("彩色打印"+getStr());
    }
}

class BlackPrinter extends Printer{
    public BlackPrinter(String str){
        super(str);
    }
    public void print(){
        System.out.println("黑白打印"+getStr());
    }
}

public class Test02 {

    public static void main(String[] args) {
        ColorPrinter cprint = new ColorPrinter("彩色打印机");
        cprint.print();
        BlackPrinter bprint = new BlackPrinter("黑白打印机");
        bprint.print();
    }
}

 

posted @ 2019-04-26 20:43  Geek张东坡  阅读(1052)  评论(0编辑  收藏  举报