W3School TIY Editor

  • W3School 在线教程
  • 改变方向
  • 暗黑模式
​x
 
class Main {
  public static void main(String[] args) {
    Pig myPig = new Pig();  // 创建 Pig 对象
    myPig.animalSound();
    myPig.sleep();
  }
}
​
// 抽象类
abstract class Animal {
  // 抽象方法(没有方法体)
  public abstract void animalSound();
  // 普通方法
  public void sleep() {
    System.out.println("Zzz");
  }
}
​
// 子类(继承自 Animal)
class Pig extends Animal {
  public void animalSound() {
    // 在这里提供 animalSound() 的方法体
    System.out.println("猪说: 哼哼");
  }
}
​