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();
}
}