当前位置:网站首页>SV class virtual method of polymorphism
SV class virtual method of polymorphism
2022-08-05 00:21:00 【Bunny9__】
SV 类的虚方法 多态 类型转换
概述
- Member methods of a class can have modifiers
virtual
(虚方法) - A virtual method is a basic polymorphic construct
- 一个虚方法可以覆盖基类的同名方法
- Declare virtual methods in parent and child classes,其方法名、参数名、The parameter directions should all be consistent
- 在调用虚方法时,它将调用句柄指向对象的方法,而不受句柄类型的影响
class BasePacket;
int A = 1;
int B = 2;
function void printA;
$display("BasePacket::A is %d", A);
endfunction
virtual function void printB;
$display("BasePacket::B is %d", B);
endfunction
endclass
class My_Packet extends BasePacket;
int A = 3;
int B = 4;
function void printA;
$display("My_Packet::A is %d", A);
endfunction
virtual function void printB;
$display("My_Packet::B is %d", B);
endfunction
endclass
module tb;
BasePacket P1 = new();
My_Packet P2 = new();
initial begin
P1.printA; // A is 1
P1.printB; // B is 2
P1 = P2; // The subclass handle is assigned to the superclass,The parent class handle points to an object of the child class
P1.printA; // A is 1
P1.printB; // B is 4
P2.printA; // A is 3
P2.printB; // B is 4
end
endmodule
By default, the parent class handle will look up and call the parent class method,When the subclass handle is assigned to the superclass,After the parent class handle points to the object of the child class,The parent class handle lookup method will be extended to the child class,If there is a method with the same name in the subclass,Then the method of the same name of the subclass will be executed.故P1.printB()
和P2.printB()
打印结果都是4.
virtual关键字的作用:Although the handle types are not the same,But calling the function will take precedence over the subclass's implementation,That is, if the subclass has a method with the same name, the method in the subclass is called.
Variables can also be declared asvirtual:不可以
- People call it a virtual method,No dummy variables
- The scope of parent class access methods can be extended to subclasses,But the variable scope accessed is only the parent class variable scope
- The above code subclass method can not be usedvirtual声明,But the parent class must be declared
- 上述代码,no parent classvirtual,子类用virtual,则不行,In this way, the parent class object is looking for a method,I don't know where to look for subclasses,Will only look in the parent class scope
Some suggested points:
- Classes are not recommended when encapsulatinglocal、protected
- When a class inherits,In order to facilitate access to more variables later,When a subclass inherits a parent class, try not to have variables with the same name
边栏推荐
- #yyds dry goods inventory #Switching equipment serious packet loss troubleshooting
- 软件测试面试题:什么是软件测试?软件测试的目的与原则?
- 软件开发工具的技术要素
- 【LeetCode】双指针题解汇总
- More than 2022 cattle school training topic Link with the second L Level Editor I
- 【unity编译器扩展之模型动画拷贝】
- 日志(logging模块)
- tiup telemetry
- GO中sync包自由控制并发的方法
- 软件测试面试题:软件测试类型都有哪些?
猜你喜欢
"Relish Podcast" #397 The factory manager is here: How to use technology to empower the law?
Cloud native - Kubernetes 】 【 scheduling constraints
gorm joint table query - actual combat
【LeetCode】滑动窗口题解汇总
The master teaches you the 3D real-time character production process, the game modeling process sharing
matlab中rcosdesign函数升余弦滚降成型滤波器
node使用redis
找不到DiscoveryClient类型的Bean
leetcode:266. 回文全排列
2 用D435i运行VINS-fusion
随机推荐
2022牛客多校训练第二场 L题 Link with Level Editor I
RK3399平台开发系列讲解(内核调试篇)2.50、嵌入式产品启动速度优化
【LeetCode】图解 904. 水果成篮
图解 Canvas 入门
论文解读( AF-GCL)《Augmentation-Free Graph Contrastive Learning with Performance Guarantee》
【云原生--Kubernetes】调度约束
2022杭电多校第一场 1004 Ball
软件测试面试题:软件测试类型都有哪些?
tiup telemetry
找不到DiscoveryClient类型的Bean
canvas 高斯模糊效果
【LeetCode】Summary of Two Pointer Problems
标识符、关键字、常量 和变量(C语言)
.net (C#) get year month day between two dates
[230]连接Redis后执行命令错误 MISCONF Redis is configured to save RDB snapshots
元宇宙:未来我们的每一个日常行为是否都能成为赚钱工具?
SV 类的虚方法 多态
could not build server_names_hash, you should increase server_names_hash_bucket_size: 32
机器学习(公式推导与代码实现)--sklearn机器学习库
Senior game modelers tell newbies, what are the necessary software for game scene modelers?