当前位置:网站首页>SystemVerilog学习-07-类的继承和包的使用
SystemVerilog学习-07-类的继承和包的使用
2022-07-01 05:45:00 【Vuko-wxh】
类的继承
什么是继承?
继承是面向对象编程范式的关键概念。类用来创建用户自定义类型. 继承使得用户可以用非常安全,非侵入的方式对类的行为进行增加或者修改。从基类做扩展并产生新的子类的过程叫类的派生,当一个类被扩展并创建之后,该派生类就继承了其基类的数据成员、属性和方法,这就是类的继承。
继承后的类可以实现以下功能:
继承了原来类的方法,并可以修改
添加新的方法
添加新的数据成员
在实现以上功能的同时需要满足一定的规则:
子类继承父类的所有数据成员和方法
子类可以添加新的数据成员和方法
子类可以重写基类中的数据成员和方法
如果一个方法被重写,其必须保持和基类的原有定义有一致的参数
子类可以通过super操作符来引用父类中的方法和成员
被声明为local的数据成员和方法只能对自己可见,对外部和子类都不可见;对声明为protected的数据成员和方法,对外部不可见,对自身和子类可见。
继承和子类
之前定义过的类Packet,可以进—步扩展构成一个新的类LinkedPacket。通过extends,LinkedPacket继承于其父类Packet,包括继承其所有的成员(变量/方法)。
class LinkedPacket extends Packet;
LinkedPacket next;
function LinkedPacket get_next();
get_next = next;
endfunction
endclass
子类可以继承父类的成员,所以LinkedPacket对象也是一个合法的Packet对象。
由此,父类句柄可以指向子类的对象。
LinkedPacket lp = new;
Packet p = lp;
关于父类和子类的句柄,可以将子类句柄直接赋值给父类句柄,子类可以拥有与父类同名的成员方法,子类可以拥有与父类同名的成员变量。
super
super是用来访问当前对象其父类的成员。尤其当子类的成员如果与父类的成员同名,那么需要使用super来指定访问其父类成员,而非默认的子类成员。
class Packet; // base class
integer value;
function integer delay();
delay = value * value;
endfunction
endclass
class LinkedPacket extends Packet; //derived class
integer value ;
function integer delay();
delay = super.delay ()+ value * super.value;
endfunction
endclass
包的使用
包的定义
为了使得可以在多个模块(硬件)或者类(软件)之间共享用户定义的类型,SV添加了包(package) 。
包的概念参考于VHDL,用户自定义的类型如类、方法、变量、结构体、枚举类等都可以在package…endpackage中定义。
导出包的内容
module、interface、class等可以使用包中定义或者声明的内容。
可以通过域的索引符::号直接引用。
definitions::parameter definitions::instruction_t inst可以指定索引一些需要的包中定义的类型到指定的域中。
module M; import definitions::instruction_t; instruction_t inst; endmodule通过通配符 * 来将包中所有的类别导入到指定的域中。
module M; import definitions::*; instruction_t inst; endmodule包和库的区分
package这个容器可以对类型做一个隔离的作用。package的意义在于将软件(类、类型、方法等)封装在不同的域中,以此来与全局的域进行隔离。
库是编译的产物,硬件( module、interface、program)都会编译到库中,如果不指定编译库的话,会被编译进入默认的库中。库可以容纳硬件类型,也可以容纳软件类型,例如类、方法和包。
包只能容纳软件类型例如类、方法和参数。
reference
- 西电路科验证PPT
边栏推荐
- Fiber Bragg grating (FBG) notes [1]: waveguide structure and Bragg wavelength derivation
- Dear pie users, I want to confess to you!
- He struggled day and night to protect his data
- 芯片,建立在沙粒上的帝国!
- 基于微信小程序的青少年生理健康知识小助手(免费获取源码+项目介绍+运行介绍+运行截图+论文)
- 为什么用葫芦儿派盘取代U盘?
- Orcle创建用户+角色
- Continuous breakthrough and steady progress -- Review and Prospect of cross platform development technology of mobile terminal
- 码蹄集 - MT3114 · 有趣的平衡 - 用样例通俗地讲解
- Mongodb学习篇:安装后的入门第一课
猜你喜欢

What is the at instruction set often used in the development of IOT devices?

Brief description of activation function

On the first day of the new year, 3000 Apache servers went down

Debug details under pycharm

Chapitre d'apprentissage mongodb: Introduction à la première leçon après l'installation

【QT】qt加减乘除之后,保留小数点后两位

我从技术到产品经理的几点体会

穿越派 你的数据云行

Crossing pie · pie pan + Mountain duck = local data management

Crossing sect · paipan + Siyuan notes = private notebook
随机推荐
boot+jsp的高校社团管理系统(附源码下载链接)
boot+jsp的高校社團管理系統(附源碼下載鏈接)
C language beginner level - realize the minesweeping game
Continue to learn MySQL
导数的左右极限和左右导数的辨析
从诺奖知“边缘计算”的未来!
Leetcode top 100 question 2 Add two numbers
bat操作ftp上传下载命令
Ssm+mysql second-hand trading website (thesis + source code access link)
【问题思考总结】为什么寄存器清零是在用户态进行的?
My experience from technology to product manager
First defined here occurs during QT compilation. Causes and Solutions
【考研高数 武忠祥+880版 自用】高数第二章基础阶段思维导图
Advanced cross platform application development (III): online resource upgrade / hot update with uni app
Some errors encountered in MySQL data migration
Call us special providers of personal cloud services for College Students
ssm+mysql二手交易网站(论文+源码获取链接)
JDBC common interview questions
Leetcode Max rectangle, Max square series 84 85. 221. 1277. 1725. (monotonic stack, dynamic programming)
Debug details under pycharm