当前位置:网站首页>2022-07-25 顾宇佳 学习笔记
2022-07-25 顾宇佳 学习笔记
2022-07-27 05:47:00 【haha姐】
多态
多态核心代码:父类型引用指向子类型对象
不管向上或者向下转型的条件:只有存在继承关系才可以转型 第一种多态形式:(向上转型) 父类 父类对象 = new 子类(); 子类型 —> 父类型 向上转型可以自动转换1. 自动转型,隐式转换,向上转型就是父类引用指向子类实例,也可以说:子类对象可以赋值 父类2.向上转型是安全的,因为子类都重写了父类方法,父类引用指向那个子类,就调用该子类重 写方法3.向上转型,父类引用只能调用子类重写方法,不能调用子类特有的方法,如果想调用,就必 须 向下转型4.父类的静态方法是不允许子类重写的第二种多态形式:(向下转型) 发生向下转型的前提,要先发生向上转型,才能通过强转再转成子类类型。 父类型 —> 子类型 向下转型需要加强制类型转换符1.强制转型 显式转换 ,向下转型就是子类的引用指父类引用,也可以说,父类强制成子类就可以调用 子类特有的方法2.转型时容易出现异常 ClassCastExcepiton的发生。3.防止这种异常的发生,需要用java所提供instanceof (对象比较运算符),给引用变量作类型的校验
- 对象名 instanceof 数据类型
- 如果对象属于该类型,就返回true
- 如果对象不属于该类型,就返回
- 向下转型后 可以调用子类特有方法,但要先向上转型
public static void main(String[] args) { 2 //需求是调用蜡笔特有的填色方法 Pen p=new WaxCrayon(); //向上转型 子转父 自动 if(p instanceof WaxCrayon){ //防止出现类型转换异常 WaxCrayon w=(WaxCrayon)p; //向下转型 父转子 强制 w.color(); } }
何时需要使用向下转型
在多态中,父类型引用子类型对象时,如果需要访问子类特有的属性或方法时,需要向下转型,否则编译无法通过。
多态的形成有3个条件 1、有继承 2、有重写 3、有父类对象指向子类引用instanceof关键字:判断某一个对象是否是某一个类的实例,返回值是boolean类型方法的重写: 重写的方法的返回值可以是被重写方法的返回值的子类。void
匿名对象
语法:new 类名();
功能:和正常的有名字的对象的功能是相同的。
依然具备了调用属性,方法的功能。
使用场景:多数是用在传参,实参,多数情况下配合构造器使用
好处:节约资源。边栏推荐
- Digital image processing -- Chapter 3 gray scale transformation and spatial filtering
- Details of cross entropy loss function in pytorch
- Significance of NVIDIA SMI parameters
- ZnS-DNA QDs近红外硫化锌ZnS量子点改性脱氧核糖核酸DNA|DNA修饰ZnS量子点
- Netease Yunxin appeared at the giac global Internet architecture conference to decrypt the practice of the new generation of audio and video architecture in the meta universe scene
- TS learning (VIII): classes in TS
- 如何借助自动化工具落地DevOps|含低代码与DevOps应用实践
- 2021 interview question of php+go for Zhongda factory (1)
- Livox Slam (with lio+ closed loop detection optimization)
- pre-commit install 时 CalledProcessError
猜你喜欢

ZnS DNA QDs near infrared zinc sulfide ZnS quantum dots modified deoxyribonucleic acid dna|dna modified ZnS quantum dots

OpenGL development with QT (I) drawing plane graphics

Reflection on pytorch back propagation

Dsgan degenerate network

如何借助自动化工具落地DevOps|含低代码与DevOps应用实践

Talk about multimodality of fire

pytorch笔记:TD3

DNA (deoxyribonucleic acid) supply | carbon nanotube nucleic acid loaded dna/rna material | dna/rna nucleic acid modified magnetic nanoparticles

Digital image processing - Chapter 6 color image processing

DNA科研实验应用|环糊精修饰核酸CD-RNA/DNA|环糊精核酸探针/量子点核酸探针
随机推荐
Reflection on pytorch back propagation
Go obtains the processing results of goroutine through channel
Drools(5):Drools高级语法
Leetcode series (I): buying and selling stocks
Image super-resolution evaluation index
Analysis on the current situation and optimization strategy of customer experience management in banking industry
Cass11.0.0.4 for autocad2010-2023 dog free usage
指令集 x 数澜科技丨加速政企数字化转型,打造DT领域独角兽企业联盟
Reasoning speed of model
MySQL quickly compares database table data
C time related operation
How to learn C language? This article gives you the complete answer
Quartus:往别人的工程添加.v文件报错
李沐动手学深度学习V2-transformer和代码实现
MySQL query operation index optimization practice
MySQL limit paging query optimization practice
Day012 application of one-dimensional array
Digital image processing Chapter 1 Introduction
Watermelon book learning notes - Chapter 4 decision tree
强网杯2021 pwn 赛题解析——babypwn