当前位置:网站首页>The difference between "rewrite" and "overload"
The difference between "rewrite" and "overload"
2022-07-25 22:48:00 【Cool hair】
Method rewriting
- Override Method rewriting : In the inheritance relationship , If a subclass defines a method with exactly the same method name and parameters as the parent class , We call it rewriting (Override), That is , The subclass has the same method declaration as the parent class .
- Application of method rewriting : When a child class needs the function of a parent class , When the subclass of function subject has its own unique content , You can override methods in the parent class , such , It follows the function of the parent class , It also defines the specific content of the subclass .
- @Override
① It's an annotation
② It can help us check the correctness of the rewrite method declaration
Method rewrite considerations :
① Private methods cannot be overridden ( Private members of a parent class cannot be inherited from a child class )
② Subclass method access rights cannot A lower ( private < Default <public)
What is overload ?
Overload Method overloading :
- Multiple methods in the same class
- Multiple methods have the same method name
- Multiple method parameters are different , Different in type or quantity
Method overload features :
- Overloads only correspond to the definition of methods , It has nothing to do with the method call , The call mode refers to the standard format
- Overloads only identify the names and parameters of methods in the same class , Independent of the return value , That is, you cannot determine whether the two methods constitute overloads by the return value
Code block 1: Does not constitute a heavy load
public class Demo01{
public static void dosth(int a){
// Method body
}
public static int dosth(int a){
// Method body
}
}Code block 2: Constitute heavy load
public class Demo02{
public static void dosth(int a){
// Method body
}
public static int dosth(int a,int b){
// Method body
}
}The purpose of method overloading : Use the same method name for methods with similar functions , Easy to remember , It is also simpler to call
Be careful : The return value types of method overloads are usually the same
summary :
in general , If the method name and parameters of the method are different , That's it (Overload) heavy load ,(Overload) heavy load The method of is a new method ; If the method name and parameters are the same , And the return value is the same , That's it (Override) rewrite .
边栏推荐
- 【集训DAY12】Minn ratio 【dfs】【最小生成树】
- Share two music playing addresses
- [PMP learning notes] Chapter 1 Introduction to PMP System
- Solve several common problems
- ECMA 262 12 Lexical Grammer
- 依法严厉打击违规自媒体运营者:净化自媒体行业迫在眉睫
- Hcie is finally in hand, and the road begins
- LabVIEW develops PCI-1680U dual port can card
- CMU AI PhD 第一年总结
- Platform architecture construction
猜你喜欢

LabVIEW 开发 PCI-1680U双端口CAN卡

Examples and points for attention about the use of getchar and scanf

Why should we launch getaverse?

Qt5.12 installation error prompt: c:\qt5.12.11\vcredist\vcredist_ msvc2019_ x86.exe /norestart /q

CUDA environment construction

新媒体运营策略(以小红书为例)帮助你快速掌握爆款创作方法

C语言逆序打印字符串的两种方法

Use of qvariant

Websocket summary

Qt中文编程遇C2001错误,提示“常量中有换行符”
随机推荐
QT的Tree View Model示例
Pyspark data analysis basis: pyspark.sql.sparksession class method explanation and operation + code display
[training day15] paint road [minimum spanning tree]
Node.js operation database
Mocha test
LabVIEW develops PCI-1680U dual port can card
1000个Okaleido Tiger首发上线Binance NFT,引发抢购热潮
CMU AI PhD 第一年总结
Floating effect and characteristics
Why should we launch getaverse?
【集训DAY11】Nescafe【贪心】
【集训DAY13】Out race【数学】【动态规划】
单元测试,写起来到底有多痛?
Matrixcube unveils the complete distributed storage system matrixkv implemented in 102-300 lines
xss-工具-Beef-Xss安装以及使用
[PMP learning notes] Chapter 1 Introduction to PMP System
Array中可以用泛型吗
Builder pattern
Today, learn about the use of lists, hyperlinks, image tags, and audio and video
Two methods of printing strings in reverse order in C language