Java接口练习

练习一

  1. 声明接口Eatable,包含抽象方法public abstract void eat();
package Test0609;

/**
 * 声明接口Eatable,包含抽象方法public abstract void eat();
 */
public interface Eatable {
    public abstract void eat();

}

  1. 声明实现类中国人chinese,重写抽象方法,打印用筷子吃饭
package Test0609;



public  class Chinesetest implements Eatable{

    public void eat(){

        System.out.println("中国人使用筷子吃饭");

    }

}

  1. 声明实现类美国人American,重写抽象方法,打印用刀叉吃饭
package Test0609;



public class American implements Eatable {

    public void eat() {

        System.out.println("美国用刀叉吃饭");

    }

}

  1. 声明实现类印度人Indian,重写抽象方法,打印用手抓饭
package Test0609;



public class Indian implements Eatable {

    public void eat(){

        System.out.println("印度使用手抓饭");

    }

}

  1. 声明测式类EatableTest,创建Eatable数组,存储各国人对象,并遍历数组,调用eat()方法
package Test0609;



public class EatableTest {

    public static void main(String[] args) {

        //数据元素类型  变量名  数据类型及个数

        Eatable[] e1 = new Eatable[3];



        //接口的多态性

        e1[0] = new Chinesetest();

        e1[1] = new American();

        e1[2] = new Indian();



        for(int i = 0;i< e1.length;i++){



            e1[i].eat();

        }

    }



}

练习二

package Test0609;

public interface CompareObject {
    //若返回值是0,代表相等;若为正常,代表当前对象大;负数代表当前对象小
    public abstract int compareto(Object o);
}

package Test0609;

public class Circle0609 {
    private double radius;//半径

    public Circle0609() {
    }

    public Circle0609(double radius) {
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    @Override
    public String toString() {
        return "Circle0609{" +
                "radius=" + radius +
                '}';
    }
}

package Test0609;



public class ComparableCircle extends Circle0609 implements CompareObject{

    public ComparableCircle(){

    }

    public ComparableCircle(double radius){

        super(radius);

    }

    //根据对象半径的大小、比较对象的大小

    public int compareto(Object o){

        if((this) == o){

            return 0;

        }

        if (o instanceof ComparableCircle){

            ComparableCircle c = (ComparableCircle)o;

            return Double.compare(this.getRadius(), c.getRadius());

        }else{

            return 2;//输入类型不匹配返回2

        }



    }

}
package Test0609;



public class InterfaceTest {

    public static void main(String[] args) {



        ComparableCircle c1 = new ComparableCircle(1.3);

        ComparableCircle c2 = new ComparableCircle(3.2);



        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一样大");

         }

    }

}
 
  

运行结果:Java接口练习_第1张图片

 

练习三:

阿里的一个工程师Developer,结构见图。

其中,有一个乘坐交通工具的方法takingVehicle(),在此方法中调用交通工具的run()。为了出行方便,他买了一辆捷安特自行车、一辆雅迪电动车和一辆奔驰轿车。这里涉及到的相关类及接口关系如图

其中,电动车增加动力的方式是充电,轿车增加动力的方式是加油。在具体交通工具的run()中调用其所在类的相关属性信息。

请编写相关代码,并测试。

提示:创建Vehicle[]数组,保存阿里工程师的三辆交通工具,并分别在工程师的takingVehicle()中调用Java接口练习_第2张图片

 

package Test0609;

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 Test0609;

public class Bicycle extends Vehicle {

    public Bicycle(){
    }
    public Bicycle(String brand,String color){
        super(brand,color);
    }

    public void run(){
        System.out.println("直行车用脚蹬行驶");
    }
}

package Test0609;

public class ElectricVeghicle extends Vehicle implements IPower{
    public ElectricVeghicle() {
    }

    public ElectricVeghicle(String brand, String color) {
        super(brand, color);
    }

    public void run(){
        System.out.println("电动车通过电机驱动行驶");
    }
    public void power(){
        System.out.println("电动车通过电力驱动");
    }
}

package Test0609;

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;
    }
    public void run(){
        System.out.println("汽车通过内燃机驱动行驶");
    }
    public void power(){
        System.out.println("汽车燃烧燃油获得能量");
    }

}

package Test0609;

public interface IPower {
    void power();
}

package Test0609;

public class VehicleTest {
    public static void main(String[] args) {
        Developer d1 = new Developer();

        //创造三个交通工具保存在数组中
        Vehicle[] v1 = new Vehicle[3];
        v1[0] = new Bicycle("捷安特","红");
        v1[1] = new ElectricVeghicle("雅迪","天蓝色");
        v1[2] = new Car("奔驰","黑色","沪");

        v1[1].run();
        for(int i = 0;i< v1.length;i++){
            v1[i].run();

            System.out.println("**********************");
            if(v1[i] instanceof IPower){
                ((IPower) v1[i]).power();
            }
        }
    }
}

你可能感兴趣的:(java,servlet,jvm)