Java的多态
2021/7/7 9:04:48
本文主要是介绍Java的多态,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Java的多态
多态的注意事项:
- 多态是方法的多态,属性没有多态
- 父类和子类,有联系 类型转换异常(ClassCastException)
- 存在的条件:继承关系,方法需要重写,父类引用指向子类对象。 Father f1 = new Son();
- 不能被重写
- static 方法,属于类,他不属于实例:
- final 常量:
- private 方法:
- 即同一方法可以根据发送对象的不同而采用多种不同的行为方式
- 一个对象的实际类型是确定的,但可以指向的引用类型有很多(父类,或者有关系的类)
- 多态存在的条件
- 有继承关系
- 子类重写父类方法
- 父类引用指向子类对象
- instanceof :(类型转换)引用类型
package com.cnblogs; //本类用于实现 public class Application { public static void main(String[] args) { //编译看左边,运行看右边 //实例化(学生是一个人) 能调用的方法都是自己重写或者继承父类的方法,子类特有的方法用不了 Persion stu = new Student(); //实例化(老师是一个人) Persion tea = new Teacher(); stu.work();//上学 stu.eat();//干饭 tea.work();//教书 //父子类都有的静态方法,使用的是父类的 stu.sleep();//父类睡觉 } }
package com.cnblogs; /* persion类 */ public class Persion { public void work(){ System.out.println("什么事都干"); } public void eat(){ System.out.println("干饭"); } public static void sleep(){ System.out.println("父类睡觉"); } }
package com.cnblogs; /* student类 */ public class Student extends Persion{ @Override public void work(){ System.out.println("上学"); } public static void sleep(){ System.out.println("子类睡觉"); } public void work2(){ System.out.println("写作业"); } }
package com.cnblogs; public class Teacher extends Persion{ @Override public void work(){ System.out.println("教书"); } }
多态的优势:
- 多态可以让我们不用关心某个对象到底具体是什么类型,就可以使用该对象的某些方法
- 提高了程序的可扩展性和可维护性
多态的使用:
- 成员变量: 使用的是父类的
- 成员方法: 由于存在重写现象,所以使用的是子类的
- 静态成员: 随着类的加载而加载,谁调用就返回谁的
多态下,子类如何调用自己特有的方法呢?
package com.cnblogs; //本类用于实现 public class Application { public static void main(String[] args) { //实例化(学生是一个人) 子类转换为父类,可能会丢失自己的本来的一些方法! Persion obj = new Student(); //强转 stu将这个对象转换为Student类型,我们就可以用Student类型的方法了。 Student stu = (Student)obj; //实例化(老师是一个人) Persion tea = new Teacher(); stu.work();//上学 stu.eat();//干饭 stu.work2();//写作业 //强转 ((Student)obj).work2();//写作业 tea.work();//教书 } }
- 父类引用指向子类的对象
- 把子类转换为父类,向上转型:
- 把父类转换为子类,向下转型:强制转换
- 方便方法的调用,减少重复的代码 简洁。
instanceof :(类型转换)引用类型
package com.cnblogs; //本类用于实现 public class Application { public static void main(String[] args) { //Object > Persion > Student //Object > Persion > Teacher //Object > String Object object = new Student(); //System.out.println(X instanceof Y);能不能编译通过! System.out.println(object instanceof Student);//true System.out.println(object instanceof Persion);//true System.out.println(object instanceof Object);//true System.out.println(object instanceof Teacher);//false System.out.println(object instanceof String);//false System.out.println("=============================================="); Persion persion = new Student(); System.out.println(persion instanceof Student);//true System.out.println(persion instanceof Persion);//true System.out.println(persion instanceof Object);//true System.out.println(persion instanceof Teacher);//false //System.out.println(person instanceof String);//编译报错! System.out.println("=============================================="); Student student = new Student(); System.out.println(student instanceof Student);//true System.out.println(student instanceof Persion);//true System.out.println(student instanceof Object);//true //System.out.println(student instanceof Teacher);//编译错误 //System.out.println(student instanceof String);//编译错误 } }
这篇关于Java的多态的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-05小米13T Pro系统合集:性能与摄影的极致融合,值得你升级的系统ROM
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求
- 2024-10-01使用 golang 将ETH账户的资产平均分散到其他账户
- 2024-10-01JWT用户校验课程:从入门到实践
- 2024-10-01Server Component课程入门指南