当前位置:网站首页>CSDN 21 Days Learning Challenge - Polymorphism (05)
CSDN 21 Days Learning Challenge - Polymorphism (05)
2022-08-10 09:36:00 【Second to admire la son!】
Event address: CSDN 21-day Learning Challenge
The biggest reason for learning is to get rid of mediocrity. One day earlier, one more wonderful life; one day later, one more day of mediocrity.Dear friends, if you:
Want to systematically/deeply learn a certain technical knowledge point...
It is difficult to persevere in groping and learning alone, and want to study efficiently in a group...
Want to write a blog but can't start, and urgently need the injection of dry goodsEnergy...
love to write, willing to make myself a better person...
…
What is polymorphism?
means that objects can have multiple shapes.
Common forms of polymorphism
superclass type object name = new subclass constructor
Member access features in polymorphism
Method call: compile on the left, run on the right.
Variable call: look on the left when compiling, and look on the left when running.(note)
Prerequisites for polymorphism
There is an inheritance/implementation relationship; there is a parent class reference pointing to a subclass object; there is method overriding (polymorphism focuses on behavioral polymorphism).
Advantages
In the polymorphic form, the object on the right can be decoupled, which is convenient for expansion and maintenance.
Animal a = new Dog();a.run(); // Subsequent business behavior changes with objects, and subsequent code does not need to be modifiedWhen defining a method, use the parent type as a parameter, and the method can receive all subclass objects of the parent class, reflecting the extensibility and convenience of polymorphism.
A problem with polymorphism:
Polymorphism cannot use the unique features of subclasses
Automatic type conversion (child to parent):
Animal c = new Cat();Coercion (from parent to child)
From parent to child (must perform forced type conversion, otherwise an error will be reported): Subclass object variable = (subclass) variable of parent class type
Function: It can solve the disadvantage of polymorphism, and can call subclassesUnique function.
Note: Classes with inheritance/implementation relationships can be casted at the compile stage; however, if the converted type and the real object type of the object are not the same type, a ClassCastException will occur when running the code
Animal c = new Cat();Dog d = (Dog)c; // An exception ClassCastException occurredJava recommends using instanceof to determine the real type of the current object before coercion conversion, and then perform coercion conversion
Variable name instanceof Real type
Determines the real type of the object pointed to by the variable on the left of the keyword, whether it is the type on the right or its subclass type, and returns true if it is, and vice versa.
Summary
1. How many ways are there for the type conversion of reference data types?
Automatic type conversion, forced type conversion.
2. What problems can be solved by forced type conversion?
Can be converted into a true subclass type, thus calling the subclass's unique functions.
3. What should be paid attention to in mandatory type conversion?
Two types with inheritance relationship/implementation can be casted, and there is no problem in compiling.
At runtime, if the casted type is found not to be the real type of the object, an error (ClassCastException) will be reported
4. What should be done before cast and how?
Use instanceof to determine the real type of the current object, and then perform forced conversion
Object variable name instanceof real type
边栏推荐
猜你喜欢

腾讯云校园大使开始招募啦,内推名额和奖金等你来拿

【 WeChat applet 】 read page navigation
![[Internet of Things Architecture] The most suitable open source database for the Internet of Things](/img/e9/10cf128dec3000daf7a3b2c816588f.jpg)
[Internet of Things Architecture] The most suitable open source database for the Internet of Things

微信小程序--》小程序生命周期和WXS使用

【系统设计】S3 对象存储

关于编程本质那些事
![How to use [jmeter regular expression extractor] to solve the problem of returning the value as a parameter](/img/bf/2367304d5bdf520e369097a49a7bb6.png)
How to use [jmeter regular expression extractor] to solve the problem of returning the value as a parameter
![[Metaverse Omi Says] Listen to how Rabbit Fan Rabbit creates a new era of trendy play from virtual to reality](/img/e8/f431a9c932e0fa5f413b1f7e09bf46.png)
[Metaverse Omi Says] Listen to how Rabbit Fan Rabbit creates a new era of trendy play from virtual to reality
![[Metaverse Omi Says] See how UCOUCO integrates performance art into the Metaverse](/img/2e/75e52bb202ae260daf23f44fa0d3e6.png)
[Metaverse Omi Says] See how UCOUCO integrates performance art into the Metaverse

WebView2 通过 PuppeteerSharp 实现爬取 王者 壁纸 (案例版)
随机推荐
CSDN21天学习挑战赛——多态(05)
Chapter 3 Search and Graph Theory (3)
腾讯发布四足机器人 Max 二代版本,梅花桩上完成跳跃、空翻
FPGA时钟篇(二) 7系列clock region详解
郭晶晶家的象棋私教,好家伙是个机器人
线程池的基本概念、结构、类
Controller层代码这么写,简洁又优雅!
重学冒泡排序
消息队列概述
以技术御风险,护航云原生 | 同创永益 X 博云举办产品联合发布会
How to understand the difference between BIO, NIO, and AIO
2022年固定资产管理系统的概况
【Software Exam System Architect】System Reliability Analysis and Design ① System Reliability Analysis
keepalived:常见问题
2022 首期线下 Workshop!面向应用开发者们的数据应用体验日来了 | TiDB Workshop Day
裸辞→自我放松→闭关→复习→斩获Offer
【元宇宙欧米说】听兔迷兔如何从虚拟到现实创造潮玩新时代
多元线性回归分析(Stata)
原型和原型链
【API Management】What is API Management and why is it important?