当前位置:网站首页>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 类名();
功能:和正常的有名字的对象的功能是相同的。
依然具备了调用属性,方法的功能。
使用场景:多数是用在传参,实参,多数情况下配合构造器使用
好处:节约资源。边栏推荐
- 基于SSM学生学籍管理系统
- Cass11.0.0.4 for autocad2010-2023 dog free usage
- Image super-resolution evaluation index
- Gbase 8C - SQL reference 6 SQL syntax (15)
- pytorch笔记:TD3
- DNA修饰贵金属纳米颗粒|脱氧核糖核酸DNA修饰纳米金(科研级)
- DDD Domain Driven Design Notes
- Drools(5):Drools基础语法(3)
- Two ways of multi GPU training of pytorch
- Web configuration software for industrial control is more efficient than configuration software
猜你喜欢

Book borrowing management system based on SSM

Dajiang livox customized format custommsg format conversion pointcloud2

Bert and RESNET can also be trained on mobile phones?!

Watermelon book learning Chapter 5 --- neural network

Dimension problems and contour lines

火狐浏览器,访问腾讯云服务器的时候,出现建立安全连接失败的问题。

PNA肽核酸修饰多肽Suc-Tyr-Leu-Val-pNA|Suc-Ala-Pro-Phe-pNA 11

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

DNA(脱氧核糖核酸)供应|碳纳米管载核酸-DNA/RNA材料|DNA/RNA核酸修饰磁性纳米颗粒

Express receive request parameters
随机推荐
ZnS DNA QDs near infrared zinc sulfide ZnS quantum dots modified deoxyribonucleic acid dna|dna modified ZnS quantum dots
DNA科研实验应用|环糊精修饰核酸CD-RNA/DNA|环糊精核酸探针/量子点核酸探针
脱氧核糖核酸DNA改性近红外二区砷化镓GaAs量子点|GaAs-DNA QDs|DNA修饰GaAs量子点
Analysis on the current situation and optimization strategy of customer experience management in banking industry
Golang encapsulates the packages involved in MySQL and the differences between sqlx and Gorm
Details of cross entropy loss function in pytorch
肽核酸PNA-多肽PNA-TPP|Glt-Ala-Ala-Pro-Leu-pNA|Suc-Ala-Pro-pNA|Suc-AAPL-pNA|Suc-AAPM-pNA
基于SSM医院预约管理系统
Digital image processing -- Chapter 3 gray scale transformation and spatial filtering
DNA research experiment application | cyclodextrin modified nucleic acid cd-rna/dna | cyclodextrin nucleic acid probe / quantum dot nucleic acid probe
How to learn C language? This article gives you the complete answer
[unity URP] the code obtains the universalrendererdata of the current URP configuration and dynamically adds the rendererfeature
nvidia-smi 各参数意义
Problems related to pytorch to onnx
A Competitive Swarm Optimizer for Large Scale Optimization
Shell编程的规范和变量
Interpretation of deepsort source code (V)
含有偶氮苯单体的肽核酸寡聚体(NH2-TNT4,N-PNAs)齐岳生物定制
Pytorch model
Day012 application of one-dimensional array