interface Friendly { void sayHello();
void sayGoodbye();
}
class Dog { // How many times this dog wags its tail when
// saying hello.
private int wagCount = ((int) (Math.random() * 5.0)) + 1; void sayHello() { System.out.print("Wag");
for (int i = 0; i < wagCount; ++i) {
System.out.print(", wag");
}
System.out.println(".");
} public String toString() { return "Woof!";
}
}class CockerSpaniel extends Dog implements Friendly { // How many times this Cocker Spaniel woofs when saying hello.
private int woofCount = ((int) (Math.random() * 4.0)) + 1; // How many times this Cocker Spaniel wimpers when saying
// goodbye.
private int wimperCount = ((int) (Math.random() * 3.0)) + 1; public void sayHello() { // Wag that tail a few times.
super.sayHello(); System.out.print("Woof");
for (int i = 0; i < woofCount; ++i) {
System.out.print(", woof");
}
System.out.println("!");
} public void sayGoodbye() { System.out.print("Wimper");
for (int i = 0; i < wimperCount; ++i) {
System.out.print(", wimper");
}
System.out.println(".");
}
}
class Cat implements Friendly {
public void eat() { System.out.println("Chomp, chomp, chomp.");
} public void sayHello() { System.out.println("Rub, rub, rub.");
} public void sayGoodbye() { System.out.println("Scamper.");
} protected void finalize() { System.out.println("Meow!");
}
}
extends是继承类是用,implements是继承接口时用。
一个类只能用一次extends,而可以用多次implements。。
继承于类Dog 同时实现Friendly 接口
extends是继承Dog类,implements是实现了Friendly接口
super.sayHello(); 是调用了父类Dog的方法sayHello
LZ没有学过java的面向对象吗?如果没有就看看java的书是怎么讲的吧
implements是实现接口,表示必须要将说实现的接口中全部的抽象方法都要给出具体实现