当前位置: 首页 > news >正文

多态 使用场景

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

场景一:方法参数多态

// 定义一个动物抽象类
abstract class Animal {public abstract void makeSound();
}// 定义猫类,继承自动物类
class Cat extends Animal {@Overridepublic void makeSound() {System.out.println("喵喵喵");}
}// 定义狗类,继承自动物类
class Dog extends Animal {@Overridepublic 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); // 传入狗对象}
}

场景二:返回值多态

// 定义一个形状抽象类
abstract class Shape {public abstract double area();
}// 定义圆形类,继承自形状类
class Circle extends Shape {private double radius;public Circle(double radius) {this.radius = radius;}@Overridepublic 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;}@Overridepublic 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());}
}

向上转型和向下转型示例

// 定义一个父类
class Parent {public void print() {System.out.println("This is Parent class");}
}// 定义一个子类
class Child extends Parent {@Overridepublic 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 关键字用于检查一个对象是否是某个类或接口的实例。

// 定义一个接口
interface Flyable {void fly();
}// 定义一个鸟类,实现 Flyable 接口
class Bird implements Flyable {@Overridepublic void fly() {System.out.println("Bird is flying");}
}// 定义一个飞机类,实现 Flyable 接口
class Plane implements Flyable {@Overridepublic 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); // 输出 trueSystem.out.println(bird instanceof Flyable); // 输出 trueSystem.out.println(plane instanceof Bird); // 输出 false}
}

http://www.dtcms.com/a/274375.html

相关文章:

  • 【构建Tomcat版本检查工具:自动检测并提醒版本更新】
  • 云、实时、时序数据库混合应用:医疗数据管理的革新与展望(中)
  • 解决Linux绑定失败地址已使用(端口被占用)的问题
  • day050-ansible剧本与变量
  • 云暴露面分析完整指南
  • 2025年7月11日—基础算法—高精度
  • 删除mysql文件夹时显示在另一程序中打开
  • 期权交易完整版教程简介
  • 工具分享--IP与域名提取工具
  • YOLOv13来了!基于超图增强的自适应视觉感知实时目标检测
  • 数据结构第一章复杂度的认识
  • WebSocket 重连与心跳机制:打造坚如磐石的实时连接
  • 005---Xilinx Viivado FIFO (二)---fifo IP核使用总结
  • python之set详谈
  • 大数据驱动的酒店用品需求预测模型研究 开发——毕业论文,毕业设计——仙盟创梦IDE
  • Linux驱动基本概念(内核态、用户态、模块、加载、卸载、设备注册、字符设备)
  • linux文件系统目录结构以及交互界面
  • 稳定币将成为新时代的重要金融工具
  • Pandas:数据类型转换
  • c99-柔性数组
  • NVME在ubuntu上总是导致死机
  • Android simpleperf生成火焰图
  • 深度解析 DApp 开发:从技术架构到商业落地的全链路解决
  • Linux 进程管理核心机制
  • 掌握Spring声明式事务传播机制:AOP与ThreadLocal的协同工作
  • 破解异构日志清洗五大难题,全面提升运维数据可观测性
  • 用FunctionCall实现文件解析(一):环境准备与基础知识
  • uniapp语音播报天气预报微信小程序
  • 秒杀系统该怎么设计?
  • uniapp-在windows上IOS真机运行(含开发证书申请流程)