3.抽象类、接口、内部类

抽象类、接口、内部类

一、抽象类

为子类提供一个通用的模版和框架,定义一些通用的逻辑或规范,同时允许子类根据需要实现具体功能。

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.
    }
}

二、接口

定义一组行为规范

  1. 接口通过抽象方法定义了一组行为规范,强制实现类实现这些方法

  2. 一个类可以实现多个接口,从而表现出多种行为

  3. 字段默认是 public static final,用于定义全局常量

  4. 表示can do what

  5. 如果一个类实现的多个接口中有同名的默认方法,需要手动解决冲突

    /* 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();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值