Java 是纯面向对象语言。类定义属性和方法,对象是类的实例。接口用于定义能力契约。

本节要点

  • 使用 class 定义类,new 创建对象。
  • extends 实现继承,implements 实现接口。
  • 多态让父类引用可以指向子类对象。

代码示例

interface Notifier {
    void send(String message);
}

class EmailNotifier implements Notifier {
    public void send(String message) {
        System.out.println("邮件:" + message);
    }
}

public class Demo {
    public static void main(String[] args) {
        Notifier notifier = new EmailNotifier();
        notifier.send("欢迎注册");
    }
}