47 lines
1.0 KiB
Java
47 lines
1.0 KiB
Java
package chapter4.Example11;
|
|
|
|
interface Animal {
|
|
int ID = 1; // 接口默认为常量添加 public static final
|
|
String NAME = "牧羊犬"; // 接口默认为常量添加 public static final
|
|
|
|
// 定义抽象方法shout(),接口默认添加 public abstract
|
|
void shout();
|
|
|
|
// 静态方法,默认添加 public
|
|
static int getID() {
|
|
return Animal.ID;
|
|
}
|
|
|
|
// 抽象方法
|
|
public void info();
|
|
}
|
|
|
|
interface Action {
|
|
public void eat();
|
|
}
|
|
|
|
class Dog implements Animal, Action {
|
|
public void eat() {
|
|
System.out.println("喜欢吃骨头");
|
|
}
|
|
|
|
// 重写Animal接口中的抽象方法shout()
|
|
public void shout() {
|
|
System.out.println("汪汪...");
|
|
}
|
|
|
|
public void info() {
|
|
System.out.println("名称:" + NAME);
|
|
}
|
|
}
|
|
|
|
public class Example11 {
|
|
public static void main(String[] args) {
|
|
System.out.println("编号:" + Animal.getID());
|
|
Dog dog = new Dog();
|
|
dog.info();
|
|
dog.shout();
|
|
dog.eat();
|
|
}
|
|
}
|