当前位置:网站首页>2022-07-25 Gu Yujia's study notes
2022-07-25 Gu Yujia's study notes
2022-07-27 07:21:00 【Sister haha】
polymorphic
Polymorphic core code : A parent type reference points to a child type object
No matter the conditions of upward or downward transformation : Only the existence of inheritance can transform The first form of polymorphism :( Upward transformation ) Parent class Parent object = new Subclass (); subtypes —> Parent type Upward transformation can automatically transform1. Automatic transformation , Implicit conversion , Upward transformation is The parent class reference points to the child class instance , It can also be said that : Subclass objects can be assigned Parent class2. It's safe to move up , Because subclasses override parent methods , The parent class reference points to that subclass , Just call this subclass Write a method3. Upward transformation , A parent class reference can only call subclass override methods , You cannot call subclass specific methods , If you want to call , It will Must be Move down4. Static methods of a parent class are not allowed to be overridden by subclassesThe second form of polymorphism :( Move down ) The premise of downward transformation , We need to make an upward transformation first , Can be converted to subclass type through strong conversion . Parent type —> subtypes Downward transformation requires mandatory type converter 1. Forced transformation Explicit conversion , Downward transformation refers to the reference of subclasses, which refers to the reference of parent classes , It can also be said that , The parent class is forced to be subclassedYou can call Subclass specific methods2. It is easy to be abnormal during transformation ClassCastExcepiton Happen .3. Prevent this kind of abnormality , Need to use java Provided instanceof ( Object comparison operators ), Make classes for reference variablesType verification
- Object name instanceof data type
- If the object belongs to this type , Just go back to true
- If the object does not belong to this type , Just go back to
- After the downward transformation You can call subclass specific methods , But first, we need to transform upwards
public static void main(String[] args) { 2 // The requirement is to call the crayon specific coloring method Pen p=new WaxCrayon(); // Upward transformation Son to father Automatically if(p instanceof WaxCrayon){ // Prevent type conversion exceptions WaxCrayon w=(WaxCrayon)p; // Move down Parent rotor mandatory w.color(); } }
When to use downward transformation
In polymorphism , When a parent type references a child type object , If you need to access subclass specific properties or methods , We need to move down , Otherwise the compilation will not pass .
The formation of polymorphism has 3 Conditions 1、 Have inherited 2、 There's rewriting 3、 There is a parent object pointing to a subclass referenceinstanceof keyword : Determine whether an object is an instance of a class , The return value is boolean typeMethod rewrite : The return value of the overridden method can be a subclass of the return value of the overridden method .void
Anonymous object
grammar :new Class name ();
function : It has the same function as normal named objects .
It still has the call attribute , Function of method .
Use scenarios : Most of them are used to transmit parameters , Actual parameters , In most cases, it is used with constructors
benefits : Saving resource .边栏推荐
- 在kettle中快速更新一个字段中的信息
- Algorithm -- Fibonacci sequence (kotlin)
- 使用popen来执行一个命令并获得返回结果
- oracle清理含有引用分区的表的数据库磁盘空间
- SQLite 常用功能整合
- Automatically generate UML sequence diagram according to text (draw.io format)
- A Competitive Swarm Optimizer for Large Scale Optimization
- Gbase 8C technical features
- Golang encapsulates the packages involved in MySQL and the differences between sqlx and Gorm
- Calledprocesserror during pre commit install
猜你喜欢

Drools (5): drools advanced syntax

美联储SR 11-7:模型风险管理指南(Guidance on Model Risk Management)-万字收藏

String类的用法

A Competitive Swarm Optimizer for Large Scale Optimization

Drools (5): drools basic syntax (3)

Bash: 创建返回布尔类型值的函数

A Competitive Swarm Optimizer for Large Scale Optimization

C4D动画如何提交云渲染农场快速渲染?

Perl: 将要执行的外部命令拆分为多行

How MySQL executes query statements
随机推荐
从技术原理看元宇宙的可能性:Omniverse如何“造”火星
C语言 pthread_cleanup_push()和pthread_cleanup_pop()函数(用于临界资源程序段中发生终止动作后的资源清理任务,以免造成死锁,临界区资源一般上锁)
把Excel转换成CSV/CSV UTF-8
MySQL limit paging query optimization practice
MySQL2
泛型 -- 学会它,好处多多
一个优先级顺序的SQL问题
Jmeter: interface automation test - BeanShell compares database data and return data
DDD Domain Driven Design Notes
Oracle database problems
美联储SR 11-7:模型风险管理指南(Guidance on Model Risk Management)-万字收藏
Codeforces Round #804 (Div. 2)(5/5)
Ci framework learning of PHP
Golang encapsulates the packages involved in MySQL and the differences between sqlx and Gorm
The possibility of metauniverse from the perspective of technical principles: how Omniverse "built" Mars
Perl: 将要执行的外部命令拆分为多行
DRConv-pytorch改称输出和输入一样的尺寸
pre-commit install 时 CalledProcessError
Internal class -- just read this article~
软件测试十大必问面试题(附答案和解析)