面向对象练习题

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", "[email protected]");
        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");
    }
}

你可能感兴趣的:(面向对象练习题)