韩顺平Java24——接口、内部类Homework

1.

 

 

/**
     * @author 紫英
     * @version 1.0
     * @discription 接口作业
     */
    public class Homework {
    
        public static void main(String[] args) {
            Person person = new Person("唐僧",new Hourse());
            person.passCommon();
            person.passRiver();
            person.passCommon();
            person.passRiver();
            person.passMontuant();
        }
    }

    interface Vehicles {
        public void work();
    }
    class Planet implements Vehicles{

        @Override
        public void work() {
            System.out.println("飞机呼呼呼。。。");
        }
    }

    class Hourse implements Vehicles {

        @Override
        public void work() {
            System.out.println("骑马哒哒哒...");
        }
    }

    class Boat implements Vehicles {

        @Override
        public void work() {
            System.out.println("划船呼呼呼...");
        }
    }

    class Factory {

        public static Hourse getHouse() {
            Hourse hourse = new Hourse();
            return hourse;
        }

        public static Boat getBoat() {
            Boat boat = new Boat();
            return boat;
        }
        public static Planet getPlanet(){
            Planet planet = new Planet();
            return planet;
        }


    }

    class Person {
        private String name;
        private Vehicles vehicles;

        public Person() {
        }

        public Person(String name, Vehicles vehicles) {
            this.name = name;
            this.vehicles = vehicles;
        }

        public Vehicles getVehicles() {

            return vehicles;
        }

        public void passRiver(){
            if (!(vehicles instanceof Boat)){
                vehicles = Factory.getBoat();
            }
            vehicles.work();
        }
        public void passCommon(){
            if (!(vehicles instanceof Hourse)){
                vehicles = Factory.getHouse();
            }

            vehicles.work();
        }
        public void passMontuant(){
            if (!(vehicles instanceof Planet)){
                vehicles = Factory.getPlanet();
            }

            vehicles.work();
        }
        public void setVehicles(Vehicles vehicles) {
            this.vehicles = vehicles;
        }

        public Person(String name, Vehicles vehicles, String state) {
            this.name = name;
            this.vehicles = vehicles;

        }
    }

2.

 

 

/**
 * @author 紫英
 * @version 1.0
 * @discription
 */
public class Homework02 {
    public static void main(String[] args) {
        Car car = new Car(-9);
        car.getAir().flow();


    }
}
class Car{
    private double temperature;

    public Car(double temperature) {
        this.temperature = temperature;
    }
    public Air getAir(){
        return new Air();
    }
    class Air{
        public void flow(){
            if (temperature>40.0){
                System.out.println("冷风呼呼呼...");
            }else if (temperature<0.0){
                System.out.println("暖风呼呼呼...");
            }else{
                System.out.println("空调已关闭");
            }

        }
    }
}

3.

 

 

package homework;

/**
 * @author 紫英
 * @version 1.0
 * @discription
 */
public class Homework03 {
    public static void main(String[] args) {
        Colour colour = Colour.RED;
        colour.show();
        switch (colour) {
            //枚举类传入switch方法
            case RED:
                System.out.println("red");
                break;
            case BLUE:
                System.out.println("BLUE");
                break;
            case BLACK:
                System.out.println("BLACK");
                break;
            case GREEN:
                System.out.println("GREEN");
                break;
            case YELLOW:
                System.out.println("YELLOW");
                break;

        }

    }
}

interface Ia {
    public void show();
}

enum Colour implements Ia {
    RED(255, 0, 0), BLUE(0, 0, 255), BLACK(0, 0, 0),
    YELLOW(255, 255, 0), GREEN(0, 255, 0);
    private int redValue;
    private int greenValue;
    private int blueValue;

    Colour(int redValue, int greenValue, int blueValue) {
        this.redValue = redValue;
        this.greenValue = greenValue;
        this.blueValue = blueValue;
    }


    @Override
    public void show() {
        System.out.println("redValue=" + redValue +
                ", greenValue=" + greenValue +
                ", blueValue=" + blueValue
        );
    }
}

 

4.

 

 

/**
 * @author 紫英
 * @version 1.0
 * @discription
 */
public class Homework01 {

    public static void main(String[] args) {
        Cellphone cellphone = new Cellphone();
        cellphone.testWork(new Calc() {
            @Override
            public double work(double n1, double n2) {
                return n1 + n2;
            }
        }, 10, 20);
    }
}

interface Calc {
    double work(double n1, double n2);

}

class Cellphone {

    public double testWork(Calc calc, double n1, double n2) {
        double res = calc.work(10, 20);
        System.out.println("结果:" + res);
        return res;

    }
}

 

posted @ 2021-12-26 20:25  紫英626  阅读(45)  评论(0编辑  收藏  举报

紫英