接口:没有实现关键字的类

时间:2012-03-04 16:30:11

标签: java

下面的类使用类接口的方法剥离而不实现它,它也不是一个抽象类隐藏方法吗?

interface Peelable {
    int peel();
}

class Fruit {
    // Return int number of pieces of peel that
    // resulted from the peeling activity.
    public int peel() {
        System.out.println("Peeling is appealing.");
        return 1;
    }
}

class Apple implements Peelable {
    private Fruit fruit = new Fruit();
    public int peel() {
        return fruit.peel();
    }
}

class FoodProcessor {
    static void peelAnItem(Peelable item) {
        item.peel();
    }
}

class Example5 {
    public static void main(String[] args) {
        Apple apple = new Apple();
        FoodProcessor.peelAnItem(apple);
    }
}

1 个答案:

答案 0 :(得分:1)

FruitPeelableApple无关(*),因为[Fruit]未实现Peelable

它们只是两个类[AppleFruit]或类和接口[FruitPeelable碰巧有一个具有相同名称的方法。

(*)不是 - 更确切的关系。显然,Apple有一个Fruit作为字段。