多态 使用场景

多态允许不同类的对象对同一消息做出不同的响应。在 Java 中,多态主要通过继承和接口实现。

场景一:方法参数多态

java 复制代码
// 定义一个动物抽象类
abstract class Animal {
    public abstract void makeSound();
}

// 定义猫类,继承自动物类
class Cat extends Animal {
    @Override
    public void makeSound() {
        System.out.println("喵喵喵");
    }
}

// 定义狗类,继承自动物类
class Dog extends Animal {
    @Override
    public void makeSound() {
        System.out.println("汪汪汪");
    }
}

// 定义一个动物操作类
class AnimalOperator {
    public void operate(Animal animal) {
        animal.makeSound();
    }
}

public class PolymorphismExample {
    public static void main(String[] args) {
        AnimalOperator operator = new AnimalOperator();
        Cat cat = new Cat();
        Dog dog = new Dog();

        operator.operate(cat); // 传入猫对象
        operator.operate(dog); // 传入狗对象
    }
}

场景二:返回值多态

java 复制代码
// 定义一个形状抽象类
abstract class Shape {
    public abstract double area();
}

// 定义圆形类,继承自形状类
class Circle extends Shape {
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    @Override
    public double area() {
        return Math.PI * radius * radius;
    }
}

// 定义矩形类,继承自形状类
class Rectangle extends Shape {
    private double width;
    private double height;

    public Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }

    @Override
    public double area() {
        return width * height;
    }
}

// 定义一个形状工厂类
class ShapeFactory {
    public Shape createShape(int type) {
        if (type == 1) {
            return new Circle(5);
        } else {
            return new Rectangle(4, 6);
        }
    }
}

public class ReturnPolymorphismExample {
    public static void main(String[] args) {
        ShapeFactory factory = new ShapeFactory();
        Shape shape1 = factory.createShape(1);
        Shape shape2 = factory.createShape(2);

        System.out.println("Shape 1 area: " + shape1.area());
        System.out.println("Shape 2 area: " + shape2.area());
    }
}

向上转型和向下转型示例

java 复制代码
// 定义一个父类
class Parent {
    public void print() {
        System.out.println("This is Parent class");
    }
}

// 定义一个子类
class Child extends Parent {
    @Override
    public void print() {
        System.out.println("This is Child class");
    }

    public void childMethod() {
        System.out.println("This is a child-specific method");
    }
}

public class TypeCastingExample {
    public static void main(String[] args) {
        // 向上转型
        Parent parent = new Child(); // 子类对象赋值给父类引用
        parent.print(); // 调用子类重写的方法

        // 向下转型
        if (parent instanceof Child) {
            Child child = (Child) parent; // 父类引用转换为子类引用
            child.print();
            child.childMethod(); // 调用子类特有的方法
        }
    }
}

instanceof 关键字使用示例

instanceof 关键字用于检查一个对象是否是某个类或接口的实例。

java 复制代码
// 定义一个接口
interface Flyable {
    void fly();
}

// 定义一个鸟类,实现 Flyable 接口
class Bird implements Flyable {
    @Override
    public void fly() {
        System.out.println("Bird is flying");
    }
}

// 定义一个飞机类,实现 Flyable 接口
class Plane implements Flyable {
    @Override
    public void fly() {
        System.out.println("Plane is flying");
    }
}

public class InstanceOfExample {
    public static void main(String[] args) {
        Flyable bird = new Bird();
        Flyable plane = new Plane();

        System.out.println(bird instanceof Bird); // 输出 true
        System.out.println(bird instanceof Flyable); // 输出 true
        System.out.println(plane instanceof Bird); // 输出 false
    }
}
相关推荐
YuTaoShao3 分钟前
【LeetCode 热题 100】24. 两两交换链表中的节点——(解法一)迭代+哨兵
java·算法·leetcode·链表
程序员的世界你不懂25 分钟前
(20)Java+Playwright自动化测试- 操作鼠标拖拽 - 上篇
java·python·计算机外设
AI360labs_atyun38 分钟前
Java在AI时代的演进与应用:一个务实的视角
java·开发语言·人工智能·科技·学习·ai
不像程序员的程序媛1 小时前
redis的一些疑问
java·redis·mybatis
知其然亦知其所以然2 小时前
Java 面试高频题:GC 到底回收了什么、怎么回收、啥时候回收?
java·后端·面试
Z_W_H_2 小时前
【SpringBoot】 整合MyBatis+Postgresql
java·spring boot·后端
nbsaas-boot2 小时前
多租户架构下的多线程处理实践指南
java·开发语言·spring
青云交2 小时前
Java 大视界 -- Java 大数据在智能医疗远程手术机器人操作数据记录与分析中的应用(342)
java·大数据·数据记录·远程手术机器人·基层医疗·跨院协作·弱网络适配
知北游天2 小时前
Linux:多线程---同步&&生产者消费者模型
java·linux·网络
钢铁男儿3 小时前
C#接口实现详解:从理论到实践,掌握面向对象编程的核心技巧
java·前端·c#