当前位置:网站首页>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
边栏推荐
- tiup uninstall
- 在线中文姓名生成工具推荐
- TinyMCE disable escape
- "WEB Security Penetration Testing" (28) Burp Collaborator-dnslog out-band technology
- gorm联表查询-实战
- 软件测试面试题:BIOS, Fat, IDE, Sata, SCSI, Ntfs windows NT?
- 软件测试面试题:软件都有多少种分类?
- Senior game modelers tell newbies, what are the necessary software for game scene modelers?
- leetcode: 267. Palindromic permutations II
- 关于我仔细检查审核过关于工作人员页面,返回一个所属行业问题
猜你喜欢
随机推荐
node使用redis
D - I Hate Non-integer Number (选数的计数dp
Cloud native - Kubernetes 】 【 scheduling constraints
QSunSync Qiniu cloud file synchronization tool, batch upload
电子行业MES管理系统的主要功能与用途
【LeetCode】双指针题解汇总
Mysql_13 事务
【Unity编译器扩展之进度条】
More than 2022 cattle school training topic Link with the second L Level Editor I
元宇宙:未来我们的每一个日常行为是否都能成为赚钱工具?
【LeetCode】矩阵模拟相关题目汇总
Software Testing Interview Questions: What's the Key to a Good Test Plan?
lua 如何 实现一个unity协程的工具
《MySQL入门很轻松》第2章:MySQL管理工具介绍
性能测试如何准备测试数据
《WEB安全渗透测试》(28)Burp Collaborator-dnslog外带技术
"Relish Podcast" #397 The factory manager is here: How to use technology to empower the law?
2022牛客多校第三场 J题 Journey
子连接中的参数传递
软件测试面试题:请你分别画出 OSI 的七层网络结构图和 TCP/IP 的四层结构图?









![[LeetCode] Summary of Matrix Simulation Related Topics](/img/80/bd71ca5211cce5805909015a642893.jpg)