当前位置:网站首页>020-024 polymorphism review
020-024 polymorphism review
2022-07-26 02:18:00 【kfdbes】
polymorphic : A reference to a parent type can point to an object of a child type .
Parent child = new Child(); When a method is invoked in a polymorphic manner , First, check whether the parent class has sing() Method , If there are no compilation errors ; If there is , Then call the subclass sing() Method .
There are two types of cast :
- Upward type conversion (upcast): For example, I will dog Type conversion to Animal type , Convert subtype to parent type . For upward type conversions , There is no need to display the specified .
- Downward type conversion (downcast): For example, will Animal Type conversion to dog type . Convert parent type to child type . For downward type conversion , You must show the specified ( You must use cast ).
abstract class (abstract class ): Use abstract The tired modified by keywords is called abstract class . The abstract class cannot be instantiated , in other words , You can't new Come up with an abstract class object ( example ).
Abstract method (abstract method): Use abstract The method modified by keyword is called abstract method . Abstract methods need to be defined in abstract classes . As opposed to abstract methods , The previously defined method is called concrete method ( There is a statement , There is realization ).
If a class contains abstract methods , Then this class must be an abstract class .
If a class is abstract , Then this class can contain concrete methods ( There is a statement , There is realization ).
If a class contains abstract methods , Then this class must be declared as abstract class , in other words , This class must be an abstract class ; conversely , If a class is abstract , Then this class can contain both abstract methods , It can also include specific methods .
In any case , As long as a class is abstract , Then this class cannot be instantiated .
Inherit the parent class in the subclass ( The parent class is an abstract class ) Under the circumstances , Then the subclass must implement all the abstract methods defined in the parent class ; otherwise , This subclass needs to declare a abstract class .
Interface (interface): The status of the interface is equivalent to class, All methods in an interface are abstract methods . When declaring a method in an interface , have access to abstract keyword , You can also not use . Usually , Will be omitted abstract keyword .( There is a statement , No implementation ).
You can think of interfaces as special abstract classes ( Abstract classes can have concrete methods , There can also be abstract methods , There can only be abstract methods in the interface , There can be no specific method ).
Class can implement interfaces , Realize the use of keywords implements Express , Represents that a class implements an interface .
A class implements an interface , Then this class must implement all the methods declared by the interface . If the class is an abstract class , Then there is no need to implement the methods in the interface .
Java It's a single inheritance , In other words, a class can only have one parent ; A class can implement multiple interfaces , Multiple interfaces are separated by commas .
polymorphic : Polymorphism , That is, the reference of the parent type can point to the object of the child type , Or a reference to an interface type can point to an instance of the class that implements the interface . The cast method between the interface and the class implementing the interface is exactly the same as that between the parent class and the child class .
Static keyword : Can be used to modify properties , It can also be used to modify methods , It can also be used to modify classes .
static Modify properties : No matter how many objects a class generates , All these objects share a unique static member variable ; An object modifies the static member variable , The value of the static member variable of other objects also changes . If a member variable is static Of , Then we can pass Class name . Member variable name The way to use it (Java It is recommended that we use this method ).
static Modification methods :static The decorated method is called static method . For static methods , You can use the class name . Method name to access .
Static methods can only inherit , Can not rewrite (Override).
final keyword :final You can modify properties 、 Method 、 class .
final decorator : When a class is final When it is modified , Indicates that the class is a terminal class , That is, it cannot be inherited .
final Modification methods : When a method is final When it is modified , Indicates that the method is a final state method , That is, it cannot be rewritten (Override) .
final Modify properties : When an attribute is final When it is modified , Indicates that the property cannot be overridden .
When final When decorating a native data type , The value representing the native data type cannot be changed ( For example, you can't 10 become 20); If final When modifying a reference type , Indicates that the reference type cannot point to other objects ; But the content of the object that the reference refers to can change .
The static code block of the class will be executed only once , It is executed when the class is loaded , Because each class will only be loaded once , All static code blocks will be executed only once ; The construction method is not , Every time an object is generated, the constructor of the class will be called , therefore new The constructor will be called once .
If there is an existing construction method in the inheritance system , There are also static code blocks , Then first execute the static code block of the topmost class , Execute all the way to the static code block of the lowest class . Be careful : Static code blocks are executed only once .
Non static member variables cannot be accessed in static methods ; You can access static member variables in static methods . Static member variables can be accessed in non static methods .
summary : Static can only access static ; Non static access to everything
Cannot be used in static methods this keyword .
边栏推荐
- Prove that perfect numbers are even
- The third question of leetcode 302 weekly Games -- query the number with the smallest k after cutting the number
- 1. Mx6ul core module use serial -rs485 test (x)
- [2020] [paper notes] growth of bi2te3/cofeb double-layer heterojunction by magnetron sputtering——
- I.MX6UL核心模块使用连载-CAN、蜂鸣器测试 (十一)
- TI AM335x工控模块网络跟文件系统NFS的实现
- 我来图书馆小程序签到流程分析
- Prometheus + process exporter + grafana monitor the resource usage of the process
- I.MX6UL核心模块使用连载-查看系统信息 (二)
- AttributeError: ‘Document‘ object has no attribute ‘pageCount‘
猜你喜欢

Business Intelligence BI full analysis, explore the essence and development trend of Bi

National standard gb28181 protocol video platform easygbs message pop-up mode optimization

Illustration of the insertion process of b+ tree
![[2021] [paper notes] 6G technology vision - otfs modulation technology](/img/50/577ad05bc16e80d1c68eec7b6da988.png)
[2021] [paper notes] 6G technology vision - otfs modulation technology

Implementation of Ti am335x industrial control module network and file system nfs

DialogRPT-Dialog Ranking Pretrained Transformers

Kaggle registration method to solve the problem of man-machine verification

一款可插拔的AM335X工控模块板载wifi模块

Tenant issues.

项目管理:精益管理法
随机推荐
Navica tool imports remote MySQL into local MySQL database
Niuke net question brushing training (I)
Li Kou daily question - day 39 -67. Binary sum
【2019】【论文笔记】基于超材料可调谐THz宽频吸收——
DialogRPT-Dialog Ranking Pretrained Transformers
[C language brush leetcode] 1462. curriculum IV (m)
I.MX6UL核心模块使用连载-查看系统信息 (二)
I.MX6UL核心模块使用连载-eMMC读写测试 (四)
[paper reading] coat: CO scale conv attentional image transformers
Implementation of Ti am335x industrial control module network and file system nfs
TI AM335x工控模块网络跟文件系统NFS的实现
[red team] att & CK - using bits services to achieve persistence
项目管理:精益管理法
1. Mx6ul core module uses serial NAND FLASH read / write test (III)
How to install opengauss manually (non om mode)
Adruino basic experimental learning (I)
Sword finger offer 28. symmetric binary tree
prometheus+blackbox-exporter+grafana 监控服务器端口及url地址
一款可插拔的AM335X工控模块板载wifi模块
2022-07-17