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

Java面向对象——多态

· 即同一个方法可以根据发送对象的不同而采用多种不同的行为方式。

· 一个对象的实际类型是确定的,但可以指向对象的引用的类型有很多(父类,有关系的类)。

· 多态存在的条件:

  1. 有继承关系;

  2. 子类重写父类的方法;

  3. 父类引用指向子类对象。

多态注意事项:
  1. 多条是方法的多态,属性没有多态;

  2. 父类和子类,有联系    类型转换异常:ClassCastException!

  注意,以下方法没有多态:
  1. static方法,属于类,不属于实例,所以该方法不能被重写;

  2. final  常量;无法重写;

  3. private方法  私有;无法重写。


代码演示:

public class Person {public void run(){System.out.println("run");}
}
public class Student extends Person{@Overridepublic void run() {System.out.println("son");}public void eat(){System.out.println("eat");}
}
import oop.demo06.Person;
import oop.demo06.Student;
//一个项目应该只存在一个main方法
public class Application {public static void main(String[] args) {//一个对象的实际类型是确定的//new Student();//new Person();//可以指向的引用类型就不确定了:父类的引用指向子类//Student子类能调用的方法都是自己的或者继承父类的Student S1 = new Student();//Person父类,可以指向子类,但是不能调用子类独有的方法Person S2 = new Student();Object S3 = new Student();//对象能执行哪些方法,主要看对象左边的类型,和右边关系不大S2.run();  //子类重写了父类的方法,就执行子类的方法S1.run();//想调用的话,用强制转换((Student) S2).eat();}
}

instanceof(类型转换)

 instanceof关键字:判断一个对象是什么类型

创建一个父类Person,子类Student、Teacher

代码演示:

    public static void main(String[] args) {//Object > Person > StudentObject object = new Student();System.out.println(object instanceof Student);  //trueSystem.out.println(object instanceof Person);  //trueSystem.out.println(object instanceof Object);  //trueSystem.out.println(object instanceof Teacher);  //falseSystem.out.println(object instanceof String);  //falseSystem.out.println("============================");Person person = new Student();System.out.println(person instanceof Student);  //trueSystem.out.println(person instanceof Person);  //trueSystem.out.println(person instanceof Object);  //trueSystem.out.println(person instanceof Teacher);  //false//System.out.println(person instanceof String);  编译报错System.out.println("============================");Student student = new Student();System.out.println(student instanceof Student);  //trueSystem.out.println(student instanceof Person);  //trueSystem.out.println(student instanceof Object);  //true//System.out.println(student instanceof Teacher);  编译报错//System.out.println(student instanceof String);  编译报错}

类型转换:

  1. 父类引用指向子类的对象;

  2. 把子类转换为父类,向上转型;可能丢失一些方法;

  3. 把父类转换为子类,向下转型;强制转换;

  4. 方便方法的调用,减少重复的代码。简洁

代码演示:(父类、子类同上)

        //类型之间的转换:父  子//高                  低Person obj = new Student();//将上面的对象转换为Student类型,就可以使用Student的方法了Student student = (Student) obj;student.go();//或((Student) obj).go();//低                   高//子类转换为父类,可能会丢失自己的一些方法Student student1 = new Student();student1.go();Person person = student1;

http://www.lryc.cn/news/349204.html

相关文章:

  • python 对矩阵与矩阵之间对应位置的元素,做softmax操作,代码实战
  • Angular前端项目在Apache httpd服务器上的部署
  • Oracle 更改数据文件位置的几种常用方式
  • 【opencv】图像畸变校正
  • Charger之二输入电压动态电源原理(VIN-DPM)
  • 【半夜学习MySQL】表结构的操作(含表的创建、修改、删除操作,及如何查看表结构)
  • 曲线救国:window 安装 docker
  • 番外篇 | 利用PyQt5+YOLOv5来搭建目标检测系统(附可视化界面+功能介绍+源代码)
  • Pascal Content数据集
  • 【Unity】使用Resources.LoadAll读取文件的顺序问题
  • pdf怎么标注红色方框?五种PDF标注红色方框方法
  • C++字符串细节,面试题06
  • AutoModelForCausalLM.from_pretrained 函数调用本地权重报错
  • 【qt】动态属性
  • Git知识点总结
  • 【数据库】数据库指令
  • 设计模式——状态模式(State)
  • 理解打包好的vue项目结构dist包
  • 24深圳杯C题18页高质量论文+可执行代码+图表
  • Midjourney Imagine API 申请及使用
  • 电子邮箱是什么?怎么申请一个电子邮箱?
  • C++ 并发编程指南(11)原子操作 | 11.4、通过内存序实现顺序模型
  • 【数据结构】栈和队列专题
  • 2024年程序员最应该关注的几件事?
  • 【初阶数据结构】单链表基础OJ题讲解
  • 基于Java的俄罗斯方块游戏的设计与实现
  • Hadoop 3.4.0+HBase2.5.8+ZooKeeper3.8.4+Hive+Sqoop 分布式高可用集群部署安装 大数据系列二
  • umi搭建react项目
  • mybatis-plus之数据源切换事务失效问题
  • vue 百度地图点击marker修改marker图片,其他marker图片不变。