疯狂java 第六章习题

1. 通过抽象类定义车类模板,然后通过抽象的车类派生拖拉机、卡车、小轿车

定义抽象的车类

package six.practice;

public abstract class Vehicle {
    //颜色
    private String color;
    //品牌
    private String brand;
    //车型,按大小分类
    private Size size;
    //长
    private double length;
    //宽
    private double width;
    //构造器
    public Vehicle(){};

    public Vehicle(String color, String brand, Size size, double length, double width) {
        this.color = color;
        this.brand = brand;
        this.size = size;
        this.length = length;
        this.width = width;
    }

    //启动
    public abstract void startUp();
    //加速
    public abstract void speedUp();
    //减速
    public abstract void speedDown();
    //刹车
    public abstract void brake();
    //开车的时间
    public abstract long driveTime();
    //开车的总路程
    public abstract double driveDistance();
    //定义计算平均速度的通用方法
    public double getAverSpeed(){
        return driveDistance()/(double)driveTime();
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public Size getSize() {
        return size;
    }

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

    public double getLength() {
        return length;
    }

    public void setLength(double length) {
        this.length = length;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public static void main(String[] args) {

        Vehicle tractor = new Tractor("黑色","哈士奇",Size.A00,3d,5d);
        Vehicle truck = new Truck("黑色","顺丰",Size.B,6d,6d);
        tractor.startUp();
        tractor.speedUp();
        tractor.speedDown();
        tractor.brake();
        System.out.println("拖拉机的平均速度为"+tractor.getAverSpeed()+"m/s");
        truck.startUp();
        truck.speedUp();
        truck.speedDown();
        truck.brake();
        System.out.println("拖拉机的平均速度为"+truck.getAverSpeed()+"m/s");
    }
}

拖拉机类

package six.practice;

public class Tractor extends Vehicle{

    public Tractor(String color, String brand, Size size, double length, double width) {
        super(color, brand, size, length, width);
    }

    @Override
    public void startUp() {
        System.out.println("拖拉机启动");
    }

    @Override
    public void speedUp() {
        System.out.println("拖拉机正在加速");
    }

    @Override
    public void speedDown() {
        System.out.println("拖拉机正在减速");
    }

    @Override
    public void brake() {
        System.out.println("拖拉机正在刹车");
    }

    @Override
    public long driveTime() {
        return 100L;
    }

    @Override
    public double driveDistance() {
        return 20000L;
    }

}

卡车类

package six.practice;

public class Truck extends Vehicle {
    public Truck(String color, String brand, Size size, double length, double width) {
        super(color, brand, size, length, width);
    }

    @Override
    public void startUp() {
        System.out.println("卡车正在启动");
    }

    @Override
    public void speedUp() {
        System.out.println("卡车正在加速");
    }

    @Override
    public void speedDown() {
        System.out.println("卡车正在减速");
    }

    @Override
    public void brake() {
        System.out.println("卡车正在刹车");
    }

    @Override
    public long driveTime() {
        return 50L;
    }

    @Override
    public double driveDistance() {
        return 30000L;
    }
}

小轿车

package six.practice;

public class Car extends Vehicle {
    public Car(String color, String brand, Size size, double length, double width) {
        super(color, brand, size, length, width);
    }

    @Override
    public void startUp() {
        System.out.println("小轿车正在启动");
    }

    @Override
    public void speedUp() {
        System.out.println("小轿车正在加速");
    }

    @Override
    public void speedDown() {
        System.out.println("小轿车正在减速");
    }

    @Override
    public void brake() {
        System.out.println("小轿车正在刹车");
    }

    @Override
    public long driveTime() {
        return 30L;
    }

    @Override
    public double driveDistance() {
        return 25000L;
    }
}

为车的大小定义了一个枚举类

package six.practice;

public enum Size {
    A00("微型车"), A0("小型车"), A("紧凑型车"), B("中型车");

    private String name;

    private Size(String name){
        this.name = name;
    }
}

2.定义一个接口,并使用匿名内部类的方式创建接口的实例

package six.practice2;

public interface MyInterface {
    void test();
}
package six.practice2;

public class Test {
    public static void main(String[] args) {
        MyInterface mi = new MyInterface() {
            @Override
            public void test() {
                System.out.println("使用匿名内部类创建一个接口的实例");
            }
        };
        mi.test();
    }
}

3.定义一个函数式接口,并使用Lambda表达式创建函数接口的实例

package six.practice3;

public interface FounctionInterface {
    void test();
}
package six.practice3;

public class Test {
    public static void main(String[] args) {
        FounctionInterface fi = ()->System.out.println("使用Lambda表达式创建接口实例");
        fi.test();
    }
}
posted @ 2019-09-06 19:11  消灭猕猴桃  阅读(153)  评论(0编辑  收藏  举报