面向对象练习题

package leif.tests;

import java.util.Calendar;
import java.util.Scanner;

import org.junit.Test;

public class OOPExercises {
    /*
     * 猜数字游戏
     * 一个类A有一个成员变量v
     * 随机给v赋一个1~100的值
     * 定义一个类对A类的成员变量v进行猜
     * 如果大了则提示大了,小了则提示小了,等于则提示猜测成功
     */
    @Test
    public void test1() {
        int v = A.getInstance().getV();
        Scanner scanner = new Scanner(System.in);

        while (true) {
            int i = scanner.nextInt();

            if (i < v) {
                System.out.println("小了");
            } else if (i > v) {
                System.out.println("大了");
            } else {
                System.out.println("猜测成功");
                break;
            }
        }

        scanner.close();
    }

    /*
     * 定义一个交通工具(Vehicle)的类
     * 其中有属性:速度(speed)、体积(size)
     * 方法:移动(move())、设置速度(setSpeed(int speed))、设置体积(setSize(int size))、加速(speedUp())、减速(speedDown())
     * 最后在测试类中的main()中实例化一个交通工具对象
     * 通过方法给它初始化speed、size的值并打印出来
     * 另外调用加速减速的方法对速度进行改变
     */
    @Test
    public void test2() {
        Vehicle vehicle = Vehicle.getInstance();
        vehicle.setSize(10);
        vehicle.setSpeed(10);
        vehicle.move();
        vehicle.speedUp();
        vehicle.move();
        vehicle.speedDown();
        vehicle.move();
    }

    /*
     * 在程序中经常要对时间进行操作但是并没有时间类型的数据,那么我们可以自己实现一个时间类来满足程序中的需要
     * 定义名为MyTime的类
     * 其中应有三个整型成员:时hour、分minute、秒second
     * 为了保证数据的安全性,这三个成员变量应声明为私有
     * 为MyTime类定义构造方法以方便创建对象时初始化成员变量
     * 再定义display方法用于将时间信息打印出来
     * 为MyTime类添加方法:addSecond(int second)、addMinute(int minute)、addHour(int hour)、subSecond(int second)、subMinute(int minute)、subHour(int hour)
     * 分别对时、分、秒进行加减运算
     */
    @Test
    public void test3() {
        MyTime myTime = MyTime.getInstance();
        System.out.println(myTime.display());
        myTime.addHour(1);
        myTime.addMinute(1);
        myTime.addSecond(1);
        System.out.println(myTime.display());
        myTime.subHour(1);
        myTime.subMinute(1);
        myTime.subSecond(1);
        System.out.println(myTime.display());
    }

    /*
     * 编写Java程序模拟简单的计算器
     * 定义名为Number的类,其中有两个整型数据成员n1和n2,应声明为私有
     * 编写构造方法赋予n1和n2初始值
     * 再为该类定义加addition、减subtration、乘multiplication、除division等公共成员方法
     * 分别对两个成员变量执行加、减、乘、除的运算
     * 在main方法中创建Number类的对象调用各个方法并显示计算结果
     */
    @Test
    public void test4() {
        Number number = Number.getInstance(3, 5);
        System.out.println(number.addition());
        System.out.println(number.subtration());
        System.out.println(number.multiplication());
        System.out.println(number.division());
    }

    /*
     * 定义一个人类Person用于显示人的姓名和年龄
     * 该类中应该有两个私有属性姓名name和年龄age
     * 定义构造方法用来初始化数据成员
     * 再定义显示display方法将姓名和年龄打印出来
     * 在main方法中创建人类的实例然后将信息显示。
     */
    @Test
    public void test5() {
        Person person = Person.getInstance("Leif", 23);
        person.display();
    }

    /*
     * 定义一个Computer类
     * 该类有一个私有成员变量品牌type
     * 提供该变量的get和set方法
     * 在main方法中设置type的值并打印出来
     */
    @Test
    public void test6() {
        Computer computer = Computer.getInstance();
        computer.setType("戴尔");
        System.out.println(computer.getType());
    }

    /*
     * 为“无名的粉”写一个类WuMingFen
     * 要求:
     * 1、有三个属性,面码:String theMa、粉的分量:int quantity、是否带汤:boolean likeSoup
     * 2、写一个构造方法以便于简化初始化过程,如WuMingFen f1 = new WuMingFen("牛肉", 3, true);
     * 3、重载构造方法使得初始化过程可以多样化,如WuMingFen f2 = new WuMingFen("牛肉", 2);
     * 4、如何使WuMingFen f3 = new WuMingFen();语句构造出来的粉的对象是酸辣面码、2两、带汤的
     * 5、写一个普通方法check()用于查看粉是否符合要求,即将对象的三个属性打印在控制台上
     */
    @Test
    public void test7() {
        WuMingFen f1 = new WuMingFen("牛肉", 3, true);
        f1.check();
        WuMingFen f2 = new WuMingFen("牛肉", 2);
        f2.check();
        WuMingFen f3 = new WuMingFen();
        f3.check();
    }

    /*
     * 定义一个名为Vehicles交通工具的基类
     * 该类中应包含String类型的成员属性brand商标和color颜色
     * 还应包含成员方法run行驶:在控制台显示“我已经开动了”和showInfo显示信息:在控制台显示商标和颜色
     * 并编写构造方法初始化其成员属性
     * 编写Car小汽车类继承于Vehicles类并编写构造方法
     * 增加int型成员属性seats座位
     * 还应增加成员方法showCar:在控制台显示小汽车的信息
     * 编写Truck卡车类继承于Vehicles类并编写构造方法
     * 增加float型成员属性load载重
     * 还应增加成员方法showTruck:在控制台显示卡车的信息
     * 在main方法中测试以上各类
     */
    @Test
    public void test8() {
        Vehicles vehicles = new Vehicles("捷安特", "黄色");
        vehicles.showInfo();
        Vehicles car = new Car("奔驰", "黑色", 4);
        ((Car)car).showCar();
        Vehicles truck = new Truck("皮卡", "白色", 10);
        ((Truck)truck).showTruck();
    }

    /*
     * 定义一个网络用户类
     * 要处理的信息有用户ID、用户密码、email地址
     * 在建立类的实例时把以上三个信息都作为构造函数的参数输入
     * 其中用户ID和用户密码是必须的,缺省的email地址是用户ID加上字符串"@gameschool.com"
     */
    @Test
    public void test9() {
        WebUser webUser1 = new WebUser(1, "123", "123@123.com");
        System.out.println(webUser1.toString());
        WebUser webUser2 = new WebUser(2, "123");
        System.out.println(webUser2.toString());
    }

    /*
     * 编写Addition类
     * 该类中应包含一组实现两数相加运算的重载方法
     * 实现加法运算的方法应接受两个参数,即加数和被加数
     * 方法将两个参数进行加法运算后返回相加结果
     * 考虑可能针对不同的数据类型进行计算,重载方法
     * 包括整型、长整型、浮点型、双精度浮点型、还有字符串
     * 在main方法中创建Addition类的实例,分别调用重载方法测试其效果
     */
    @Test
    public void test10() {
        Addition addition = Addition.getInstance();
        System.out.println(addition.add(1, 2));
        System.out.println(addition.add(3L, 4L));
        System.out.println(addition.add(5.0F, 6.0F));
        System.out.println(addition.add(7.0, 8.0));
        System.out.println(addition.add("9", "0"));
    }

    /*
     * 建立一个汽车类
     * 包括轮胎个数、汽车颜色、车身重量等属性
     * 并通过不同的构造方法创建事例
     * 至少要求汽车能够加速、减速、停车
     * 再定义一个小汽车类继承汽车类
     * 并添加空调、CD等属性
     * 覆盖加速、减速、停车的方法
     */
    @Test
    public void test11() {
        Automobile automobile = new Automobile(4, "Black", 50);
        automobile.speedUp();
        automobile.speedDown();
        automobile.stop();
        LittleAutomobile littleAutomobile = new LittleAutomobile(4, "Write", 50, "格力", "云烟成雨");
        littleAutomobile.speedUp();
        littleAutomobile.speedDown();
        littleAutomobile.stop();
    }

    /*
     * 创建一个名称为StaticDemo的类
     * 并声明一个静态变量和一个普通变量
     * 对变量分别赋予10和5的初始值
     * 在main()方法中输出变量值。
     */
    @Test
    public void test12() {
        System.out.println("x = " + StaticDemo.x);
        System.out.println("y = " + new StaticDemo().y);
    }

    /*
     * 创建一个父类和子类
     * 父类有一个数据成员
     * 子类继承父类
     * 通过子类构造函数初始化并显示该数据成员的值。
     */
    @Test
    public void test13() {
        Sun sun = new Sun(10);
        System.out.println(sun.toString());
    }

    /*
     * 创建一个Vehicle2类并将它声明为抽象类
     * 在Vehicle2类中声明一个numberOfWheels方法,使它返回一个字符串值
     * 创建两个类Car2和Motorbike2继承于Vehicle2类并在这两个类中实现numberOfWheels方法
     * 在Car2类中应当显示“四轮车”
     * 而在Motorbike类中应当显示“双轮车”
     * 创建Car2和Motorbike2的实例并在控制台中显示信息
     */
    @Test
    public void test14() {
        Car2 car2 = Car2.getInstance();
        car2.numberOfWheels();
        Motorbike2 motorbike2 = Motorbike2.getInstance();
        motorbike2.numberOfWheels();
    }

    /*
     * 创建一个名称为Vehicle的接口
     * 在接口中分别添加带有一个参数的方法start()和stop()
     * 在两个名称分别为Bike和Bus的类中实现Vehicle接口
     * 创建Bike和Bus对象并访问start()和stop()方法
     */
    @Test
    public void test15() {
        Bike bike = Bike.getInstance();
        bike.start("Bike");
        bike.stop("Bike");
        Bus bus = Bus.getInstance();
        bus.start("Bus");
        bus.stop("Bus");
    }

    /*
     * 设计一张抽象的门Door
     * 那么对于这张门来说就应该拥有所有门的共性:开门openDoor()和关门closeDoor()
     * 然后对门进行另外的功能设计:防盗theftProof()、防水waterProof()、防弹bulletProof()
     * 实例化一个拥有所有功能的门
     */
    @Test
    public void test16() {
        Door door = DoorDemo.getInstance();
        door.openDoor();
        door.closeDoor();
        ((DoorDemo)door).theftProof();
        ((DoorDemo)door).waterProof();
        ((DoorDemo)door).bulletProof();
    }

    /*
     * 设计一个纯净水生产线
     * 目前流程是从某个地方把水取出来,然后经过缓冲、过滤、加热、放糖等步骤
     */
    @Test
    public void test17() {
        Water water = PurifiedWater.getInstance();
        water.water();
        ((PurifiedWater)water).buffer();
        ((PurifiedWater)water).filter();
        ((PurifiedWater)water).heating();
        ((PurifiedWater)water).sugar();
        ((PurifiedWater)water).purifiedWater();
    }

    /*
     * 定义一个抽象的Role类
     * 有姓名、年龄、性别等成员变量
     * 要求尽可能隐藏所有变量(能够私有就私有,能够保护就不要公有)
     * 再通过GetXXX()和SetXXX()方法对各变量进行读写
     * 具有一个抽象的play()方法
     * 该方法不返回任何值
     * 同时至少定义两个构造方法
     * Role类中要体现出this的几种用法。
     * 从Role类派生出一个Employee类
     * 该类具有Role类的所有成员,构造方法除外
     * 并扩展salary成员变量
     * 同时增加一个静态成员变量“职工编号 ID ”
     * 同样要有至少两个构造方法
     * 要体现出this和super的几种用法
     * 还要求覆盖play()方法
     * 并提供一个final sing()方法
     * Manager类继承Employee类
     * 有一个final成员变量vehicle
     * 创造Manager和Employee对象,并测试这些对象的方法
     */
    @Test
    public void test18() {
        Manager manager = new Manager("Berton", 37, "male", 100000);
        manager.display();
        manager.sing();
        manager.play();
        Employee employee = new Employee("Leif", 23, "male", 7500);
        employee.display();
        employee.sing();
        employee.play();

        Role role = new Role("Hellen", 22, "female") {
            @Override
            public void play() {
                System.out.println("Role play");
            }
        };

        role.display();
        role.play();
    }

    /*
     * 1、建立一个Java抽象类Drink
     * a)声明一个抽象方法taste(),该方法负责输出饮料的味道
     * b)声明int型常量来代表不同的饮料类型:咖啡、啤酒、牛奶
     * c)声明静态工厂方法getDrink(int drinkType),根据传入的参数创建不同的饮料对象并返回该对象,建议使用switch语句
     * 2、建立Drink的具体子类
     * a)分别建立Drink的子类Coffee代表咖啡、Beer代表啤酒、Milk代表牛奶
     * b)实现taste()方法,要求在控制台打印各自的味道特征
     * 3、建立异常类DrinkNotFoundException
     * a)继承Exception
     * b)在Drink的方法getDrink(int drinkType)中声明引发DrinkNotFoundException异常,当没有相对应的饮料类型时抛出该异常
     * c)在使用getDrink方法的类中捕捉该异常
     * 4、建立Test测试类,测试以上内容的正确性
     * a)编写main方法,通过命令行传参的方式传入某种饮料的类型
     * b)在main方法中调用Drink类的getDrink方法,获得相应的饮料对象,注意捕获DrinkNotFoundException
     * c)调用饮料对象的taste()方法,输出该饮料的味道
     * 5、编译程序并运行
     */
    @Test
    public void test19() {
        Scanner scanner = new Scanner(System.in);
        Drink drink = null;

        try {
            drink = Drink.getDrink(scanner.nextInt());
        } catch (DrinkNotFoundException e) {
            e.drinkNotFound();
        }

        drink.taste();
        scanner.close();
    }
}

class A {
    private int v;

    private A(int v) {
        this.v = v;
    }

    private static volatile A a = null;

    public static A getInstance() {
        if (a == null) {
            synchronized (A.class) {
                if (a == null) {
                    a = new A((int)(Math.random() * 100 + 1));
                }
            }
        }

        return a;
    }

    public int getV() {
        return v;
    }
}

class Vehicle {
    private int speed;
    private int size;

    private Vehicle() {}

    private static volatile Vehicle vehicle = null;

    public static Vehicle getInstance() {
        if (vehicle == null) {
            synchronized (Vehicle.class) {
                if (vehicle == null) {
                    vehicle = new Vehicle();
                }
            }
        }

        return vehicle;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public void speedUp() {
        speed += 1;
    }

    public void speedDown() {
        speed -= 1;
    }

    public void move() {
        System.out.println("体积:" + size + ",速度:" + speed + ",正在移动");
    }
}

class MyTime {
    private int hour;
    private int minute;
    private int second;

    private MyTime(int hour, int minute, int second) {
        this.hour = hour;
        this.minute = minute;
        this.second = second;
    }

    private static volatile MyTime myTime = null;

    public static MyTime getInstance() {
        if (myTime == null) {
            synchronized (MyTime.class) {
                if (myTime == null) {
                    Calendar calendar = Calendar.getInstance();
                    myTime = new MyTime(calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
                }
            }
        }

        return myTime;
    }

    public String display() {
        return "MyTime [hour=" + hour + ", minute=" + minute + ", second=" + second + "]";
    }

    public void addSecond(int second) {
        this.second += second;
    }

    public void addMinute(int minute) {
        this.minute += minute;
    }

    public void addHour(int hour) {
        this.hour += hour;
    }

    public void subSecond(int second) {
        this.second -= second;
    }

    public void subMinute(int minute) {
        this.minute -= minute;
    }

    public void subHour(int hour) {
        this.hour -= hour;
    }
}

class Number {
    private int n1;
    private int n2;

    private Number(int n1, int n2) {
        this.n1 = n1;
        this.n2 = n2;
    }

    private static volatile Number number = null;

    public static Number getInstance(int n1, int n2) {
        if (number == null) {
            synchronized (Number.class) {
                if (number == null) {
                    number = new Number(n1, n2);
                }
            }
        }

        return number;
    }

    public int addition() {
        return n1 + n2;
    }

    public int subtration() {
        return n1 - n2;
    }

    public int multiplication() {
        return n1 * n2;
    }

    public double division() {
        return (double)n1 / n2;
    }
}

class Person {
    private String name;
    private int age;

    private Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    private static volatile Person person = null;

    public static Person getInstance(String name, int age) {
        if (person == null) {
            synchronized (Person.class) {
                if (person == null) {
                    person = new Person(name, age);
                }
            }
        }

        return person;
    }

    public void display() {
        System.out.println("Person [name=" + name + ", age=" + age + "]");
    }
}

class Computer {
    private String type;

    private Computer() {}

    private static volatile Computer computer = null;

    public static Computer getInstance() {
        if (computer == null) {
            synchronized (Computer.class) {
                if (computer == null) {
                    computer = new Computer();
                }
            }
        }

        return computer;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}

class WuMingFen {
    private String theMa;
    private int quantity;
    private boolean likeSoup;

    public WuMingFen() {
        theMa = "酸辣";
        quantity = 2;
        likeSoup = true;
    }

    public WuMingFen(String theMa, int quantity) {
        this.theMa = theMa;
        this.quantity = quantity;
    }

    public WuMingFen(String theMa, int quantity, boolean likeSoup) {
        this.theMa = theMa;
        this.quantity = quantity;
        this.likeSoup = likeSoup;
    }

    public void check() {
        System.out.println("WuMingFen [theMa=" + theMa + ", quantity=" + quantity + ", likeSoup=" + likeSoup + "]");
    }
}

class Vehicles {
    private String brand;
    private String color;

    public Vehicles(String brand, String color) {
        this.brand = brand;
        this.color = color;
    }

    public void run() {
        System.out.println("我已经开动了");
    }

    public void showInfo() {
        System.out.println("Vehicles [brand=" + brand + ", color=" + color + "]");
    }
}

class Car extends Vehicles {
    private int seats;

    public Car(String brand, String color, int seats) {
        super(brand, color);
        this.seats = seats;
    }

    public void showCar() {
        super.showInfo();
        System.out.println("Car [seats=" + seats + "]");
    }
}

class Truck extends Vehicles {
    private float load;

    public Truck(String brand, String color, float load) {
        super(brand, color);
        this.load = load;
    }

    public void showTruck() {
        super.showInfo();
        System.out.println("Truck [load=" + load + "]");
    }
}

class WebUser {
    private int id;
    private String password;
    private String email;

    public WebUser(int id, String password, String email) {
        this.id = id;
        this.password = password;
        this.email = email;
    }

    public WebUser(int id, String password) {
        this.id = id;
        this.password = password;
        email = id + "@gameschool.com";
    }

    @Override
    public String toString() {
        return "WebUser [id=" + id + ", password=" + password + ", email=" + email + "]";
    }
}

class Addition {
    private Addition() {}

    private static volatile Addition addition = null;

    public static Addition getInstance() {
        if (addition == null) {
            synchronized (Addition.class) {
                if (addition == null) {
                    addition = new Addition();
                }
            }
        }

        return addition;
    }

    public int add(int a, int b) {
        return a + b;
    }

    public long add(long a, long b) {
        return a + b;
    }

    public float add(float a, float b) {
        return a + b;
    }

    public double add(double a, double b) {
        return a + b;
    }

    public String add(String a, String b) {
        return a + b;
    }
}

class Automobile {
    private int tyre;
    private String color;
    private double weight;

    public Automobile(int tyre, String color, double weight) {
        this.tyre = tyre;
        this.color = color;
        this.weight = weight;
    }

    public void speedUp() {
        System.out.println("Automobile [tyre=" + tyre + ", color=" + color + ", weight=" + weight + "] speed up");
    }

    public void speedDown() {
        System.out.println("Automobile [tyre=" + tyre + ", color=" + color + ", weight=" + weight + "] speed down");
    }

    public void stop() {
        System.out.println("Automobile [tyre=" + tyre + ", color=" + color + ", weight=" + weight + "] stop");
    }
}

class LittleAutomobile extends Automobile {
    private String airConditioner;
    private String cd;

    public LittleAutomobile(int tyre, String color, double weight, String airConditioner, String cd) {
        super(tyre, color, weight);
        this.airConditioner = airConditioner;
        this.cd = cd;
    }

    @Override
    public void speedUp() {
        super.speedUp();
        System.out.println("LittleAutomobile [airConditioner=" + airConditioner + ", cd=" + cd + "] speed up");
    }

    @Override
    public void speedDown() {
        super.speedDown();
        System.out.println("LittleAutomobile [airConditioner=" + airConditioner + ", cd=" + cd + "] speed down");
    }

    @Override
    public void stop() {
        super.stop();
        System.out.println("LittleAutomobile [airConditioner=" + airConditioner + ", cd=" + cd + "] stop");
    }
}

class StaticDemo {
    public static int x = 10;
    public int y = 5;
}

class Father {
    private int i;

    public Father(int i) {
        this.i = i;
    }

    @Override
    public String toString() {
        return "Father [i=" + i + "]";
    }
}

class Sun extends Father {
    public Sun(int i) {
        super(i);
    }
}

abstract class Vehicle2 {
    public void numberOfWheels() {
        System.out.println("车轮数量");
    }
}

class Car2 extends Vehicle2 {
    private Car2() {}

    private static volatile Car2 car2 = null;

    public static Car2 getInstance() {
        if (car2 == null) {
            synchronized (Car2.class) {
                if (car2 == null) {
                    car2 = new Car2();
                }
            }
        }

        return car2;
    }

    @Override
    public void numberOfWheels() {
        super.numberOfWheels();
        System.out.println("四轮车");
    }
}

class Motorbike2 extends Vehicle2 {
    private Motorbike2() {}

    private static volatile Motorbike2 motorbike2 = null;

    public static Motorbike2 getInstance() {
        if (motorbike2 == null) {
            synchronized (Motorbike2.class) {
                if (motorbike2 == null) {
                    motorbike2 = new Motorbike2();
                }
            }
        }

        return motorbike2;
    }

    @Override
    public void numberOfWheels() {
        super.numberOfWheels();
        System.out.println("双轮车");
    }
}

interface Vehicle3 {
    public abstract void start(String ss);

    public abstract void stop(String ss);
}

class Bike implements Vehicle3 {
    private Bike() {}

    private static volatile Bike bike = null;

    public static Bike getInstance() {
        if (bike == null) {
            synchronized (Bike.class) {
                if (bike == null) {
                    bike = new Bike();
                }
            }
        }

        return bike;
    }

    @Override
    public void start(String ss) {
        System.out.println(ss + " start");
    }

    @Override
    public void stop(String ss) {
        System.out.println(ss + " stop");
    }
}

class Bus implements Vehicle3 {
    private Bus() {}

    private static volatile Bus bus = null;

    public static Bus getInstance() {
        if (bus == null) {
            synchronized (Bike.class) {
                if (bus == null) {
                    bus = new Bus();
                }
            }
        }

        return bus;
    }

    @Override
    public void start(String ss) {
        System.out.println(ss + " start");
    }

    @Override
    public void stop(String ss) {
        System.out.println(ss + " stop");
    }
}

abstract class Door {
    public void openDoor() {
        System.out.println("开门");
    }

    public void closeDoor() {
        System.out.println("关门");
    }
}

interface TheftProof {
    public abstract void theftProof();
}

interface WaterProof {
    public abstract void waterProof();
}

interface BulletProof {
    public abstract void bulletProof();
}

class DoorDemo extends Door implements TheftProof, WaterProof, BulletProof {
    private DoorDemo() {}

    private static volatile DoorDemo doorDemo = null;

    public static DoorDemo getInstance() {
        if (doorDemo == null) {
            synchronized (DoorDemo.class) {
                if (doorDemo == null) {
                    doorDemo = new DoorDemo();
                }
            }
        }

        return doorDemo;
    }

    @Override
    public void theftProof() {
        System.out.println("防盗");
    }

    @Override
    public void waterProof() {
        System.out.println("防水");
    }

    @Override
    public void bulletProof() {
        System.out.println("防弹");
    }
}

abstract class Water {
    public void water() {
        System.out.println("水");
    }
}

interface Buffer {
    public abstract void buffer();
}

interface Filter {
    public abstract void filter();
}

interface Heating {
    public abstract void heating();
}

interface Sugar {
    public abstract void sugar();
}

class PurifiedWater extends Water implements Buffer, Filter, Heating, Sugar {
    private PurifiedWater() {}

    private static volatile PurifiedWater purifiedWater = null;

    public static PurifiedWater getInstance() {
        if (purifiedWater == null) {
            synchronized (PurifiedWater.class) {
                if (purifiedWater == null) {
                    purifiedWater = new PurifiedWater();
                }
            }
        }

        return purifiedWater;
    }

    public void purifiedWater() {
        System.out.println("纯净水");
    }

    @Override
    public void sugar() {
        System.out.println("放糖");
    }

    @Override
    public void heating() {
        System.out.println("加热");
    }

    @Override
    public void filter() {
        System.out.println("过滤");
    }

    @Override
    public void buffer() {
        System.out.println("缓冲");
    }
}

abstract class Role {
    private String name;
    private int age;
    private String gender;

    public Role() {}

    public Role(String name, int age, String gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public void display() {
        System.out.println("Role [name=" + name + ", age=" + age + ", gender=" + gender + "]");
    }

    public abstract void play();
}

class Employee extends Role {
    private static int id;
    private int salary;

    public Employee() {}

    public Employee(String name, int age, String gender, int salary) {
        super(name, age, gender);
        this.salary = salary;
    }

    public static int getId() {
        return id;
    }

    public static void setId(int id) {
        Employee.id = id;
    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

    @Override
    public void display() {
        super.display();
        System.out.println("Employee [salary=" + salary + "]");
    }

    @Override
    public void play() {
        System.out.println("Employee play");
    }

    public final void sing() {
        System.out.println("Employee sing");
    }
}

class Manager extends Employee {
    private final String VEHICLE = "Car";

    public Manager() {}

    public Manager(String name, int age, String gender, int salary) {
        super(name, age, gender, salary);
    }

    @Override
    public void display() {
        super.display();
        System.out.println("Manager [vehicle=" + VEHICLE + "]");
    }

    @Override
    public void play() {
        System.out.println("Manager play");
    }
}

abstract class Drink {
    private static final int COFFEE = 1;
    private static final int BEER = 2;
    private static final int MILK = 3;

    public static Drink getDrink(int drinkType) throws DrinkNotFoundException {
        switch (drinkType) {
            case COFFEE:
                return new Coffee();
            case BEER:
                return new Beer();
            case MILK:
                return new Milk();
            default:
                throw new DrinkNotFoundException();
        }
    }

    public abstract void taste();
}

class Coffee extends Drink {
    @Override
    public void taste() {
        System.out.println("Coffee");
    }
}

class Beer extends Drink {
    @Override
    public void taste() {
        System.out.println("Beer");
    }
}

class Milk extends Drink {
    @Override
    public void taste() {
        System.out.println("Milk");
    }
}

class DrinkNotFoundException extends Exception {
    private static final long serialVersionUID = 1L;

    public void drinkNotFound() {
        System.out.println("Drink not found");
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,839评论 6 482
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,543评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 153,116评论 0 344
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,371评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,384评论 5 374
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,111评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,416评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,053评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,558评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,007评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,117评论 1 334
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,756评论 4 324
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,324评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,315评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,539评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,578评论 2 355
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,877评论 2 345

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,600评论 18 139
  • Hey,大家好,我叫大头宇,是一名全职捡屎官,喜欢写字。我老公叫大牛腩,官比我大,是我的上级领导,沉迷画画。我们家...
    大头宇阅读 381评论 0 0
  • 下午接孩子回家,必经一个十字路口。虽然是个地图上未必找不到的小镇,可是车流量却大的令人无法忍受,堵车更是堪比一线城...
    素衣烟霞阅读 603评论 1 1
  • 2017.2.13晨起感恩 累计38 感恩上师及诸佛菩萨的护佑和加持 感恩历代宗亲的传承 感恩老妈养育之恩,感恩...
    鹊曾阅读 114评论 0 1
  • 女孩子22岁后没有了老师的指导,也慢慢脱离了父母的呵护,自己要独立开始慢慢找工作了,22-23是大多数女生离开校园...
    女神萌萌哒阅读 493评论 0 0