chapter 4

This commit is contained in:
2024-10-23 12:51:53 +08:00
parent 8737a64402
commit 761fcb4905
10 changed files with 280 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
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();
}
}