当前位置:网站首页>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
边栏推荐
- Senior game modelers tell newbies, what are the necessary software for game scene modelers?
- tiup telemetry
- oracle创建用户以后的权限问题
- 网站最终产品页使用单一入口还是多入口?
- 数据类型及输入输出初探(C语言)
- 软件测试面试题:测试生命周期,测试过程分为几个阶段,以及各阶段的含义及使用的方法?
- node使用redis
- Handwritten Distributed Configuration Center (1)
- E - Distance Sequence (前缀和优化dp
- gorm的Raw与scan
猜你喜欢
随机推荐
gorm joint table query - actual combat
TinyMCE禁用转义
NMS原理及其代码实现
Huggingface入门篇 II (QA)
Software testing interview questions: test life cycle, the test process is divided into several stages, and the meaning of each stage and the method used?
E - Many Operations (bitwise consideration + dp thought to record the result after the operation
2022 Multi-school Second Session K Question Link with Bracket Sequence I
canvas 高斯模糊效果
Couple Holding Hands [Greedy & Abstract]
leetcode:267. 回文排列 II
ARC129E Yet Another Minimization 题解 【网络流笔记】
oracle创建表空间
【云原生--Kubernetes】Pod控制器
软件测试面试题:做好测试计划的关键是什么?
leetcode:269. 火星词典
10 种常见的BUG分类
could not build server_names_hash, you should increase server_names_hash_bucket_size: 32
【无标题】
00、数组及字符串常用的 API(详细剖析)
How to automatically push my new articles to my fans (very simple, can't learn to hit me)
![[idea] idea configures sql formatting](/img/89/98cd23aff3e2f15ecb489f8b3c50e9.png)








