当前位置:网站首页>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
边栏推荐
- ARC129E Yet Another Minimization 题解 【网络流笔记】
- Modelers experience sharing: model study method
- leetcode经典例题——单词拆分
- 【LeetCode】Summary of Two Pointer Problems
- The applicable scenarios and common product types of the KT148A electronic voice chip ic solution
- 2022 Hangzhou Electric Multi-School 1004 Ball
- [idea] idea configures sql formatting
- .net (C#) get year month day between two dates
- RK3399平台开发系列讲解(内核调试篇)2.50、嵌入式产品启动速度优化
- lua 如何 实现一个unity协程的工具
猜你喜欢

TinyMCE disable escape

数据类型及输入输出初探(C语言)

Modelers experience sharing: model study method

【LeetCode】滑动窗口题解汇总

Essential knowledge for entry-level 3D game modelers

TinyMCE禁用转义

2022 Niu Ke Summer Multi-School Training Camp 5 (BCDFGHK)

oracle创建表空间

【论文笔记】—低照度图像增强—Unsupervised—EnlightenGAN—2019-TIP

【LeetCode】图解 904. 水果成篮
随机推荐
Flask框架 根据源码分析可扩展点
could not build server_names_hash, you should increase server_names_hash_bucket_size: 32
could not build server_names_hash, you should increase server_names_hash_bucket_size: 32
【LeetCode】图解 904. 水果成篮
.net (C#) get year month day between two dates
网站最终产品页使用单一入口还是多入口?
Software testing interview questions: What are the strategies for system testing?
2022牛客多校训练第二场 J题 Link with Arithmetic Progression
uinty lua 关于异步函数的终极思想
关于我仔细检查审核过关于工作人员页面,返回一个所属行业问题
TinyMCE disable escape
【idea】idea配置sql格式化
机器学习(公式推导与代码实现)--sklearn机器学习库
【Valentine's Day special effects】--Canvas realizes full screen love
matlab中rcosdesign函数升余弦滚降成型滤波器
tiup telemetry
10 个关于 Promise 和 setTimeout 知识的面试题,通过图解一次说透彻
软件测试面试题:LoadRunner 分为哪三个模块?
GO中sync包自由控制并发的方法
#yyds dry goods inventory #Switching equipment serious packet loss troubleshooting