抽象类、接口、内部类
一、抽象类
为子类提供一个通用的模版和框架,定义一些通用的逻辑或规范,同时允许子类根据需要实现具体功能。
1、抽象类不能被实例化。
2、抽象类应该至少有一个抽象方法,否则它没有任何意义。
3、抽象类中的抽象方法没有方法体。
4、抽象类的子类必须给出父类中的抽象方法的具体实现,除非该子类也是抽象类。
/* by yours.tools - online tools website : yours.tools/zh/dnsusa.html */ // 抽象类 Animal abstract class Animal { protected String name; // 构造方法 public Animal(String name) { this.name = name; } // 抽象方法:子类必须实现 public abstract void sound(); // 普通方法:所有子类共享 public void sleep() { System.out.println(name + " is sleeping."); } } // 子类 Dog 继承自 Animal class Dog extends Animal { // 构造方法 public Dog(String name) { super(name); } // 实现抽象方法 @Override public void sound() { System.out.println(name + " says: Woof!"); } } // 子类 Cat 继承自 Animal class Cat extends Animal { // 构造方法 public Cat(String name) { super(name); } // 实现抽象方法 @Override public void sound() { System.out.println(name + " says: Meow!"); } } // 测试类 Main public class Main { public static void main(String[] args) { // 创建 Dog 对象 Animal dog = new Dog("Buddy"); dog.sound(); // 输出:Buddy says: Woof! dog.sleep(); // 输出:Buddy is sleeping. System.out.println(); // 创建 Cat 对象 Animal cat = new Cat("Kitty"); cat.sound(); // 输出:Kitty says: Meow! cat.sleep(); // 输出:Kitty is sleeping. } }二、接口
定义一组行为规范
接口通过抽象方法定义了一组行为规范,强制实现类实现这些方法
一个类可以实现多个接口,从而表现出多种行为
字段默认是
public static final,用于定义全局常量表示can do what
如果一个类实现的多个接口中有同名的默认方法,需要手动解决冲突
/* by yours.tools - online tools website : yours.tools/zh/dnsusa.html */ // 定义接口 Flyable interface Flyable { // 静态常量(全局常量) int MAX_SPEED = 1000; // 默认是 public static final // 抽象方法:所有实现类必须实现 void fly(); // 默认方法(Java 8 引入):提供默认实现 default void land() { System.out.println("Landing..."); } // 静态方法(Java 8 引入):通过接口名调用 static void info() { System.out.println("This is the Flyable interface."); } } // 实现类 Bird class Bird implements Flyable { private String name; public Bird(String name) { this.name = name; } @Override public void fly() { System.out.println(name + " is flying in the sky with a max speed of " + Flyable.MAX_SPEED + " km/h."); } @Override public void land() { System.out.println(name + " is landing gracefully."); } } // 实现类 Airplane class Airplane implements Flyable { private String model; public Airplane(String model) { this.model = model; } @Override public void fly() { System.out.println(model + " is flying at high altitude with a max speed of " + Flyable.MAX_SPEED + " km/h."); } } // 测试类 Main public class Main { public static void main(String[] args) { // 调用静态方法 Flyable.info(); // 输出:This is the Flyable interface. // 访问静态变量 System.out.println("Max speed for all Flyable objects: " + Flyable.MAX_SPEED + " km/h."); System.out.println(); // 创建 Bird 对象 Flyable bird = new Bird("Sparrow"); bird.fly(); // 输出:Sparrow is flying in the sky with a max speed of 1000 km/h. bird.land(); // 输出:Sparrow is landing gracefully. System.out.println(bird.MAX_SPEED + " km/h."); //1000 km/h -> 这种写法不会报错,但它实际上是 语法糖 ,编译器会自动将其转换为通过接口名访问的形式: System.out.println(Flyable.MAX_SPEED + " km/h."); System.out.println(); // 创建 Airplane 对象 Flyable airplane = new Airplane("Boeing 747"); airplane.fly(); // 输出:Boeing 747 is flying at high altitude with a max speed of 1000 km/h. airplane.land(); // 输出:Landing...(使用默认实现) } }
三、抽象类和接口的区别
| 特性 | 接口 | 抽象类 |
|---|---|---|
| 定义方式 | 使用interface关键字定义 | 使用abstract关键字定义 |
| 成员变量 | 只能是public static final | 可以是普通变量或静态变量 |
| 构造器 | 不允许定义构造器 | 可以定义构造器 |
| 多重继承 | 支持多重实现 | 不支持多重继承 |
| 设计目的 | 定义行为规范(can-do) | 定义通用结构(is-a) |
四、内部类
根据自己想限定的作用范围,来决定使用哪种。
- 成员内部类
- 静态嵌套类
- 局部内部类
- 匿名内部类-> 就是没有名字的类
public class Main { // 成员内部类(非静态) class MemberInnerClass { public void display() { System.out.println("This is a member inner class."); } } // 静态嵌套类 static class StaticNestedClass { public void display() { System.out.println("This is a static nested class."); } } // 外部类方法 public void createLocalAndAnonymousClasses() { // 局部内部类 class LocalInnerClass { public void display() { System.out.println("This is a local inner class."); } } // 创建局部内部类对象并调用方法 LocalInnerClass localInner = new LocalInnerClass(); localInner.display(); // 匿名内部类 Runnable anonymousInner = new Runnable() { @Override public void run() { System.out.println("This is an anonymous inner class."); } }; // 调用匿名内部类的方法 anonymousInner.run(); } // 测试主方法 public static void main(String[] args) { // 创建外部类对象 Main outer = new Main(); // 创建成员内部类对象并调用方法 Main.MemberInnerClass memberInner = outer.new MemberInnerClass(); memberInner.display(); // 创建静态嵌套类对象并调用方法 Main.StaticNestedClass staticNested = new Main.StaticNestedClass(); staticNested.display(); // 调用方法以创建局部内部类和匿名内部类 outer.createLocalAndAnonymousClasses(); } }