当前位置:网站首页>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 modified
When 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 occurred
Java 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
边栏推荐
- shell iterates over folders and outputs
- 俄罗斯宣布临时禁止进口摩尔多瓦植物产品
- 不要把公司当成家,被通知裁员时会变得不幸...
- 10 【异步组件 组合式函数(hooks)】
- SQL优化总结
- Nvidia's gaming graphics card revenue plummets / Google data center explosion injures 3 people / iPhone battery percentage returns... More news today is here...
- OLTP and OLAP database architecture 】 【 : actual comparison
- 【元宇宙欧米说】听兔迷兔如何从虚拟到现实创造潮玩新时代
- 【微服务架构】为故障设计微服务架构
- 郭晶晶家的象棋私教,好家伙是个机器人
猜你喜欢
J9 Number Theory: Macro Analysis of DAO Characteristics
Nvidia's gaming graphics card revenue plummets / Google data center explosion injures 3 people / iPhone battery percentage returns... More news today is here...
shell之函数和数组
2022-08-09 第六小组 瞒春 学习笔记
shell遍历文件夹并输出
shell------常用小工具,sort,uniq,tr,cut
JWT:拥有我,即拥有权力
[System Design] S3 Object Storage
Flink快速上手 完整使用 (第二章)
初识Flink 完整使用 (第一章)
随机推荐
傅立叶级数与傅里叶变换
Fourier series and Fourier transform
13 【script setup 总结】
"Microservice Architecture" Arrangement and Choreography - Different Models for Making Systems Work Together
ARM Architecture 3: Addressing and Exception Handling of ARM Instructions
DeepFake换脸诈骗怎么破?让他侧个身
[OAuth2] Nineteen, OpenID Connect dynamic client registration
2022 首期线下 Workshop!面向应用开发者们的数据应用体验日来了 | TiDB Workshop Day
【 WeChat applet 】 read page navigation
[Metaverse Omi Says] Listen to how Rabbit Fan Rabbit creates a new era of trendy play from virtual to reality
go web之cookie
J9 digital science: Web 3.0 is about data ownership or decentralized?
DataStream API(基础篇) 完整使用 (第五章)
Numpy学习
Tencent releases the second-generation version of the quadruped robot Max, which completes jumps and somersaults on the plum blossom pile
2022-08-09 第六小组 瞒春 学习笔记
[Internet of Things Architecture] The most suitable open source database for the Internet of Things
幂次方(暑假每日一题 20)
【API架构】使用 JSON API 的好处
CSDN21天学习挑战赛——多态(05)