当前位置:网站首页>The difference between overload and override
The difference between overload and override
2022-07-22 15:35:00 【bleauchat】
heavy load overload
Purpose of overloading : Save naming resources in classes and improve code readability .
Overloading occurs in a class ,
1、 Same method name
2、 The parameters of the method are different ( Number of parameters or parameter type )
3、 The return value type of the method does not affect overloading
( When two methods have the same name , The parameters are the same , But when the return value is different , Does not constitute a heavy load )
class Test {
public static void main(String[] args) {
int a = 10;
int b = 20;
int ret = add(a, b);
System.out.println("ret = " + ret);
}
public static int add(int x, int y) {
return x + y;
}
public static double add(int x, int y) {
return x + y;
}
}
// Here are two add Same parameters for , The return value type is different , Constitute heavy load
rewrite override
Rewriting means The subclass implements the method with the same name as the parent class , Method parameters and return value types are exactly the same ;
Rewriting occurs between a subclass and a superclass , That is to say, rewriting is not in the same class ;
When a subclass inherits a parent class , The method of the subclass is the same as that of the parent ( Access qualifier , return type , Method name , parameter list ), Different methods , The method of this subclass overrides the method of the parent class, which is called rewriting ;
class Animal {
public void eat(String food) {
...
}
}
class Bird extends Animal {
@Override
public void eat(String food) {
...
}
}
// here Bird Medium eat Yes Animal Medium eat Rewrite
// There are the same methods in the inheritance of parent and child classes , The only difference is the method body ,
// Generally, the method of the parent class cannot meet the needs of the child class, so it is rewritten .
Be careful :
1. Rewriting and overloading are completely different , Don't mix up.
2. Common methods can override ,static Modified static methods cannot override
3. The access permission of the method overriding the subclass cannot be lower than that of the parent class
The differences between overloading and rewriting are summarized in the following table :
N | difference | heavy load | rewrite |
---|---|---|---|
1 | Concept | The method name is the same , The type and number of parameters are different | Method name 、 return type 、 The type and number of parameters are exactly the same |
2 | Range | One class | Inheritance relationships |
3 | Limit | There is no permission requirement | The overridden method cannot have more strict access control permissions than the parent class |
Back to our systemverilog , I mentioned about method addition before virtual The question of modification , In short, it's in Methods in the parent class are rewrite , Add do not add virtual The difference of decoration has been explained above ;
And for phase for , Every component Of phase All right. uvm_component Of phase Rewriting of , And in uvm_component It's all added in virtual modification , So at least stay Method This level sv There is no overload in, only rewriting (override)!! Errors will be reported as follows :
class A;
virtual function int dis(string str="A");
$display("this is %s",str);
dis=1;
endfunction
endclass
class B extends A;
function void dis(); // Only the function name is the same , Different parameters
$display("this is B");
endfunction
endclass
Many say , It's like uvm_primer It's written in yes new To overload , This point will be discussed later !!
边栏推荐
- EACCES: permission denied, unlink ‘/usr/local/bin/code‘
- 重载(overload)和重写(override)的区别
- 深度学习之 8 深度模型优化与正则化
- AutoLabel (AutoLabel)
- cookie和session
- Graffiti Wi Fi & ble SoC development slide strip (6) -- slide strip function demonstration
- Cloud native
- vlc报错——“live555 error: no data received in 10s, aborting”
- Kubernetes基础部分学习笔记
- AIDL总结
猜你喜欢
pyside2做个简易的浏览器
PLT draw and save the results
Reasons for driving voltage deviation caused by high voltage differential probe
Tensorboard安装与使用
Tdengine learning notes
Kubernetes basic part learning notes
[information collection] write data from fofa API interface into txt and excel
JS advanced ES6 implementation inheritance
如何用一手数据可视化获得老板的青睐,抓住数据可视化关键点
【07】函数调用:为什么会发生stack overflow?
随机推荐
Bi analytical thinking of business intelligence: cash cycle of manufacturing industry (II)
How Linux queries Oracle error logs
Aidl example
海康、大华、宇视拉实时流url规则总结
Autocomplete (autocomplete)
JS advanced ES6 implementation inheritance
Tdengine experimental cluster establishment success
LeetCode 0814. 二叉树剪枝
TiDB中规划的集群包括6个DB,3个pd,3个kv,应用去连的时候,是连哪个地址?
The cluster planned in tidb includes 6 dB, 3 PD and 3 kV. When the application is disconnected, which address is it connected to?
Delete Nan points in 3D point cloud TXT file
Codeworks 5 questions per day (average 1500) - day 22
FPGA image processing learning face recognition
[07] function call: why does stack overflow happen?
Succès de la construction du cluster expérimental tdengine
K-means clustering modeling and programming
卷妹的面试小抄每日更新Day1
Leetcode 0128. longest continuous sequence
A simple inventory management system source code
Leetcode 0814. binary tree pruning