-5
以下のクラスFruitは、クラスインタフェースのメソッドpeelを実装せずに使用します。また、抽象クラスでもありません。インタフェース:implementaionキーワードを持たないクラス
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);
}
}
また、私はその質問を理解していません。 「フルーツ」は、何かを「伸ばす」または「実行しない」具体的なクラスです。だから問題は何ですか? –
「フルーツはクラスインタフェースのメソッドピールを使用します」 - いいえ、それはちょうど同じ名前を持つ無関係なメソッドを実装します! 'Fruit'はあなたのコードに' Peelable'を実装していません。 – DNA
また、「Apple」はなぜフィールドとして「Fruit」を持っていますか?本当に意味をなさない... – DNA