练习一
1、声明接口Eatable,包含抽象方法public abstract void eat(); 2、声明实现类中国人Chinese,重写抽象方法,打印用筷子吃饭 3、声明实现类美国人American,重写抽象方法,打印用刀叉吃饭 4、声明实现类印度人Indian,重写抽象方法,打印用手抓饭 5、声明测试类EatableTest,创建Eatable数组,存储各国人对象,并遍历数组,调用eat()方法
package chapter08_oop3_teacher.src.com.atguigu08._interface.exer1;/*** ClassName: Eatable* Description:* 声明接口Eatable,包含抽象方法public abstract void eat();* @Author 尚硅谷-宋红康* @Create 8:49* @Version 1.0*/ public interface Eatable {void eat(); }
package chapter08_oop3_teacher.src.com.atguigu08._interface.exer1;/*** ClassName: Chinese* Description:** @Author 尚硅谷-宋红康* @Create 8:50* @Version 1.0*/ public class Chinese implements Eatable{@Overridepublic void eat() {System.out.println("中国人使用筷子吃饭");} }
package chapter08_oop3_teacher.src.com.atguigu08._interface.exer1;/*** ClassName: American* Description:** @Author 尚硅谷-宋红康* @Create 8:51* @Version 1.0*/ public class American implements Eatable{@Overridepublic void eat() {System.out.println("美国人使用刀叉吃饭");} }
package chapter08_oop3_teacher.src.com.atguigu08._interface.exer1;/*** ClassName: Indian* Description:** @Author 尚硅谷-宋红康* @Create 8:51* @Version 1.0*/ public class Indian implements Eatable{@Overridepublic void eat() {System.out.println("印度人使用手抓饭");} }
package chapter08_oop3_teacher.src.com.atguigu08._interface.exer1;/*** ClassName: EatableTest* Description:* 声明测试类EatableTest,创建Eatable数组,存储各国人对象,并遍历数组,调用eat()方法* @Author 尚硅谷-宋红康* @Create 8:51* @Version 1.0*/ public class EatableTest {public static void main(String[] args) {Eatable[] eatables = new Eatable[3];eatables[0] = new Chinese(); //多态性eatables[1] = new American();eatables[2] = new Indian();for (int i = 0; i < eatables.length; i++) {eatables[i].eat();}} }
练习二
定义一个接口用来实现两个对象的比较。interface CompareObject{//若返回值是 0 , 代表相等; 若为正数,代表当前对象大;负数代表当前对象小public int compareTo(Object o); }定义一个Circle类,声明radius属性,提供getter和setter方法定义一个ComparableCircle类,继承Circle类并且实现CompareObject接口。 在ComparableCircle类中给出接口中方法compareTo的实现体,用来比较两个圆的半径大小。定义一个测试类InterfaceTest,创建两个ComparableCircle对象,调用compareTo方法比较两个类的半径大小。拓展:参照上述做法定义矩形类Rectangle和ComparableRectangle类,在ComparableRectangle类 中给出compareTo方法的实现,比较两个矩形的面积大小。
package chapter08_oop3_teacher.src.com.atguigu08._interface.exer2;/*** ClassName: Circle* Description:** @Author 尚硅谷-宋红康* @Create 8:57* @Version 1.0*/ public class Circle {private double radius;//半径public Circle() {}public Circle(double radius) {this.radius = radius;}public double getRadius() {return radius;}public void setRadius(double radius) {this.radius = radius;}@Overridepublic String toString() {return "Circle{" +"radius=" + radius +'}';} }
package chapter08_oop3_teacher.src.com.atguigu08._interface.exer2;/*** ClassName: ComparableCircle* Description:** @Author 尚硅谷-宋红康* @Create 8:58* @Version 1.0*/ public class ComparableCircle extends Circle implements CompareObject{public ComparableCircle() {}public ComparableCircle(double radius) {super(radius);}//根据对象的半径的大小,比较对象的大小@Overridepublic int compareTo(Object o) {if(this == o){return 0;}if(o instanceof ComparableCircle){ComparableCircle c = (ComparableCircle)o;//错误的 // return (int) (this.getRadius() - c.getRadius());//正确的写法1: // if(this.getRadius() > c.getRadius()){ // return 1; // }else if(this.getRadius() < c.getRadius()){ // return -1; // }else{ // return 0; // }//正确的写法2:return Double.compare(this.getRadius(),c.getRadius());}else{return 2; //如果输入的类型不匹配,则返回2 // throw new RuntimeException("输入的类型不匹配");}} }
package chapter08_oop3_teacher.src.com.atguigu08._interface.exer2;/*** ClassName: CompareObject* Description:** @Author 尚硅谷-宋红康* @Create 8:55* @Version 1.0*/ public interface CompareObject {//若返回值是 0 , 代表相等; 若为正数,代表当前对象大;负数代表当前对象小public int compareTo(Object o); }
package chapter08_oop3_teacher.src.com.atguigu08._interface.exer2;/*** ClassName: InterfaceTest* Description:** @Author 尚硅谷-宋红康* @Create 9:05* @Version 1.0*/ public class InterfaceTest {public static void main(String[] args) {ComparableCircle c1 = new ComparableCircle(2.3);ComparableCircle c2 = new ComparableCircle(5.3);int compareValue = c1.compareTo(c2);if(compareValue > 0){System.out.println("c1对象大");}else if(compareValue < 0){System.out.println("c2对象大");}else{System.out.println("c1和c2一样大");}} }
练习三
阿里的一个工程师Developer,结构见图。其中,有一个乘坐交通工具的方法takingVehicle(),在此方法中调用交通工具的run()。 为了出行方便,他买了一辆捷安特自行车、一辆雅迪电动车和一辆奔驰轿车。这里涉及到的相关类及接口关系如图。其中,电动车增加动力的方式是充电,轿车增加动力的方式是加油。在具体交通工具的run()中调用其所在类 的相关属性信息。请编写相关代码,并测试。提示:创建Vehicle[]数组,保存阿里工程师的三辆交通工具,并分别在工程师的takingVehicle()中调用。
package chapter08_oop3_teacher.src.com.atguigu08._interface.exer3;/*** ClassName: Developer* Description:** @Author 尚硅谷-宋红康* @Create 9:10* @Version 1.0*/ public class Developer {private String name;private int age;public Developer() {}public Developer(String name, int age) {this.name = name;this.age = age;}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 void takingVehicle(Vehicle vehicle){vehicle.run();} }
package chapter08_oop3_teacher.src.com.atguigu08._interface.exer3;/*** ClassName: Bicycle* Description:** @Author 尚硅谷-宋红康* @Create 9:13* @Version 1.0*/ public class Bicycle extends Vehicle{public Bicycle() {}public Bicycle(String brand, String color) {super(brand, color);}@Overridepublic void run() {System.out.println("自行车通过人力脚蹬行驶");} }
package chapter08_oop3_teacher.src.com.atguigu08._interface.exer3;/*** ClassName: Car* Description:** @Author 尚硅谷-宋红康* @Create 9:17* @Version 1.0*/ public class Car extends Vehicle implements IPower{private String carNumber;public Car() {}public Car(String brand, String color, String carNumber) {super(brand, color);this.carNumber = carNumber;}public String getCarNumber() {return carNumber;}public void setCarNumber(String carNumber) {this.carNumber = carNumber;}@Overridepublic void run() {System.out.println("汽车通过内燃机驱动行驶");}@Overridepublic void power() {System.out.println("汽车通过汽油提供动力");} }
package chapter08_oop3_teacher.src.com.atguigu08._interface.exer3;/*** ClassName: ElectricVehicle* Description:** @Author 尚硅谷-宋红康* @Create 9:15* @Version 1.0*/ public class ElectricVehicle extends Vehicle implements IPower{public ElectricVehicle() {}public ElectricVehicle(String brand, String color) {super(brand, color);}@Overridepublic void run() {System.out.println("电动车通过电机驱动行驶");}@Overridepublic void power() {System.out.println("电动车使用电力提供动力");} }
package chapter08_oop3_teacher.src.com.atguigu08._interface.exer3;/*** ClassName: IPower* Description:** @Author 尚硅谷-宋红康* @Create 9:18* @Version 1.0*/ public interface IPower {void power(); }
package chapter08_oop3_teacher.src.com.atguigu08._interface.exer3;/*** ClassName: Vehicle* Description:** @Author 尚硅谷-宋红康* @Create 9:12* @Version 1.0*/ public abstract class Vehicle {private String brand;//品牌private String color;//颜色public Vehicle() {}public Vehicle(String brand, String color) {this.brand = brand;this.color = color;}public String getBrand() {return brand;}public void setBrand(String brand) {this.brand = brand;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}public abstract void run(); }
package chapter08_oop3_teacher.src.com.atguigu08._interface.exer3;/*** ClassName: VehicleTest* Description:** @Author 尚硅谷-宋红康* @Create 9:21* @Version 1.0*/ public class VehicleTest {public static void main(String[] args) {Developer developer = new Developer();//创建三个交通工具,保存在数组中Vehicle[] vehicles = new Vehicle[3];vehicles[0] = new Bicycle("捷安特","骚红色");vehicles[1] = new ElectricVehicle("雅迪","天蓝色");vehicles[2] = new Car("奔驰","黑色","沪Au888");for (int i = 0;i < vehicles.length;i++){developer.takingVehicle(vehicles[i]);if(vehicles[i] instanceof IPower){((IPower) vehicles[i]).power();}}} }