当前位置:网站首页>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
边栏推荐
猜你喜欢

ARM Architecture 3: Addressing and Exception Handling of ARM Instructions

【Enterprise Architecture】Agile and Enterprise Architecture: Strategic Alliance

90.(cesium之家)cesium高度监听事件

iwemeta metaverse: Ali's first COO: how to build a sales force

07 【动态组件 组件注册】

【 WeChat applet 】 read page navigation

CTFSHOW七夕杯web

FPGA时钟篇(三) MRCC和SRCC的区别

Flink运行时架构 完整使用 (第四章)

2022 首期线下 Workshop!面向应用开发者们的数据应用体验日来了 | TiDB Workshop Day
随机推荐
[System Design] S3 Object Storage
J9 Digital Theory: What kind of sparks will Web3.0+ Internet e-commerce cause?
裸辞→自我放松→闭关→复习→斩获Offer
浅谈DAO+DeFi+NFT模式开发代码技术方案丨链游元宇宙NFT盲盒项目技术开发逻辑(源码程序)
【REST架构】OData、JsonAPI、GraphQL 有什么区别?
win下的开发环境变量记录
go web之cookie
【数据架构】分布式数据网格作为集中式数据单体的解决方案
keepalived:双主配置
Chapter 3 Search and Graph Theory (3)
10 【异步组件 组合式函数(hooks)】
「微服务架构」编曲与编舞——让系统协同工作的不同模式
CAD to WPF: Tips on converting CAD drawing files to WPF vector code files (xaml files)
jq封装树形下拉选择框组件
幂次方(暑假每日一题 20)
shell iterates over folders and outputs
【元宇宙欧米说】听兔迷兔如何从虚拟到现实创造潮玩新时代
CAD转WPF: 关于CAD图纸文件转换为WPF矢量代码文件(xaml文件)的技巧
shell------常用小工具,sort,uniq,tr,cut
Optimistic and pessimistic locking