SV中的关键词使用virtual,cast

news/2024/7/7 20:45:06

virtual

SV不允许一个子类句柄指向父类对象,但是可以让一个父类句柄指向子类对象。由于父类句柄只能访问父类的成员变量和方法,不利于验证环境的复用;所以为了让继承了同一父类的子类能将一个同名方法扩展为不同功能的方法,利用类的多态,将父类中的方法声明为virtual,而指向子类对象的句柄就可以根据指向的对象类型,而不是句柄类型来调用我们希望调用的方法。

class father;
    function display();
        $display("This is Father!!);
    endfunction
endclass

class son extends father;
    function display();
        $display("This is Son!!");
    endfunction
endclass

module tb;
    father        father_inst;
    son            son_inst;

    intial begin
        son_inst    = new();
        father_inst    = son_inst; //父类句柄指向子类对象
        father_inst.display();
        son_inst.display();
    end
endmodule
View Code

结果

This is Father!!
This is Son!!

class father;
    virtual function display();
        $display("This is Father!!);
    endfunction
endclass

class son extends father;
    virtual function display();
        $display("This is Son!!");
    endfunction
endclass

module tb;
    father        father_inst;
    son            son_inst;

    intial begin
        son_inst    = new();
        father_inst    = son_inst;
        father_inst.display();
        son_inst.display();
    end
endmodule
View Code

结果

This is Son!!

This is Son!!

从上述两个代码中发现:
1)不使用virtual,父类句柄虽指向子类对象,但调用的仍是父类本身的函数
2)使用virtual,父类句柄指向子类对象,调用的是子类的函数

类似上述总结结论:

1)声明虚方法时,根据对象来决定调用
2)未声明虚方法时,根据句柄来决定调用

cast

father_inst = new();
son_inst = father_inst; //子类直接指向父类(编译报错)
$cast(son_inst,father_inst); //直接使用cast向下转换(编译报错)

正确方式

son        son1,son2;
father    father_inst;
son1 = new();
father_inst = son1;
$cast(son2,father_inst);

 


http://lihuaxi.xjx100.cn/news/823484.html

相关文章

算法刷题 Day 31 | ● 455.分发饼干 ● 376. 摆动序列 ● 53. 最大子序和

贪心算法其实就是没有什么规律可言,所以大家了解贪心算法 就了解它没有规律的本质就够了。 不用花心思去研究其规律, 没有思路就立刻看题解。 基本贪心的题目 有两个极端,要不就是特简单,要不就是死活想不出来。 学完贪心之后再去看动态规划,就会了解贪心和动规的区别。…

朴素贝叶斯代码实现python

P(B)称为"先验概率",即在A事件发生之前,对B事件概率的一个判断。P(B|A)称为"后验概率",即在A事件发生之后,对B事件概率的重新评估。P(A|B)/P(A)称为"可能性函数",这是一个调整因子&…

Python入门第一天:Python的简单介绍

最近初学者越来越多了,于是决定更新一套Python基础教程帮助大家学习,喜欢的小伙伴可以点个关注持续学习哈! 什么是 Python? 自20世纪90年代初Python语言诞生至今,它已被逐渐广泛应用,Python 已然成为最受…

【前端】Vue项目:旅游App-(21)detail:房屋详情页及其数据、返回导航栏、轮播图

文章目录目标过程与代码请求数据:requeststore轮播图组件bug:undefined轮播图相关属性和样式返回导航栏效果总代码修改或添加的文件css/commonservice/modules/detailservice/indexstore/modules/detailviews/detail/detailviews/detail/cpns/detail-swi…

flask教程3:视图函数的路由

文章目录一、视图函数的路由规则设置说明1app.url_map 查看所有路由2 app.route("/",methods["方法"]) 更改访问方法3 同一路由装饰多个视图函数4 同一视图函数配置多个路由5 使用url_for 进行反解析二、路由提取参数与自定义路由转换器三、自定义路由转换…

HTML5+CSS3(十)-全面详解(学习总结---从入门到深化)

Display ​编辑 元素隐藏属性对比 学习效果反馈 文档流 文档流产生的问题 空格折叠 元素无空隙 脱离文档流 学习效果反馈 浮动 浮动的定义 元素向左浮动 元素向右浮动 所有元素向左浮动 学习效果反馈 Display display 可以修改元素的类型,让块…

Centos7安装JDK11图解

目录一、官网下载二、卸载再带的jdk(该操作请在root模式下进行)三、安装JDK11(该操作请在root模式下进行)一、官网下载 官网下载地址:https://www.oracle.com/java/technologies/javase-jdk11-downloads.html 二、卸…

elasticsearch_exporter安装教程

一、下载elasticsearch_exporter二进制文件压缩包 1、在节点上执行命令下载 wget "https://github.com/justwatchcom/elasticsearch_exporter/releases/download/v1.1.0/elasticsearch_exporter-1.1.0.linux-amd64.tar.gz" 2、解压压缩包 [rootmaster elasticse…