当前位置:网站首页>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
边栏推荐
- Defending risks with technology and escorting cloud native | Tongchuang Yongyi X Boyun held a joint product launch conference
- 俄罗斯宣布临时禁止进口摩尔多瓦植物产品
- ARM Architecture 3: Addressing and Exception Handling of ARM Instructions
- 09 【Attributes继承 provide与inject】
- Excel绘制统计图
- 乐观锁与悲观锁
- 浅析JWT安全问题
- JVM探究
- 设计分享|基于单片机的从左到右流水灯
- Basic concepts of concurrency, operations, containers
猜你喜欢
随机推荐
腾讯发布四足机器人 Max 二代版本,梅花桩上完成跳跃、空翻
关于编程本质那些事
Chapter 3 Search and Graph Theory (3)
The Generation of Matlab Symbolic Functions and the Calculation of Its Function Values
【微服务架构】为故障设计微服务架构
jq封装树形下拉选择框组件
消息队列概述
并查集学习
在“企业通讯录”的盲区,融云的边界与分寸
win下的开发环境变量记录
并发的基本概念,操作,容器
CSDN21天学习挑战赛——多态(05)
英伟达游戏显卡营收暴跌/ 谷歌数据中心爆炸致3人受伤/ iPhone电量百分比回归…今日更多新鲜事在此...
【数据仓库】什么是 Azure Synapse,它与 Azure Data Bricks 有何不同?
【元宇宙欧米说】听兔迷兔如何从虚拟到现实创造潮玩新时代
第三章 搜索与图论(三)
多线程浅谈
Oracle rac所在的网络要割接,停掉其中一个rac节点,这种方案可行吗?
【API架构】使用 JSON API 的好处
关于判断单峰数组的几种方法









