systemverilog 基础

systemverilog 面向对象编程(1)(封装Class,继承 Extend,多态 Polymorphism)

1. class 的定义,声明,实例化

	定义:class的定义使用静态空间,一个class 由成员变量和方法method 组成。
	声明:class 定义后,通过声明一个handle句柄,用来作为调用实例化对象的一个handle,句柄的类型由class的类型决定,后续会对父类和子类等句柄进行介绍。
	实例化:使用动态空间,通过构造函数 new(),class的实例化对象会在动态内存中开辟一片空间,handle作为这个空间的钥匙,可以调用和修改成员变量和method,当没有指针指向该对象时,systemverilog会清理掉这个实例化对象,释放内存空间,不需要析构函数及手动释放。

2. 父类,子类,virtual,$cast(), copy

2.1 父类,子类

class animal;
int feet;
string name;
string color;
function print

class birds;
	int feet;
	string name;
	string color;
	function print;
		print("the bird name is s%",name);
	endfunction
endclass : birds
class parrot extend bird;
	int feet = 2;// 子类可以继承父类的成员变量和成员函数
	string name = bird;
	int num_wings= 2; // 子类新定义的成员变量,父类(父类声明的handle不能直接访问)
	function fly; // 子类新定义的成员方法
		print("the parrot could fly because it has d% wings",num_wings);
	endfunction
endclass : parrot

2.2 virtual

virtual: 用于类,接口和方法在定义时的类型声明,
virtual声明的类不能直接实例化,必须要子类继承才能实例化,常用于声明一些标准的类,如virtual_base_seq.
virtual声明的method 子类可以重新定义,并覆盖父类已经定义的方法。可以让子类和父类实现同名但不同功能的方法。

system verilog 允许使用 2 种构造方法来创建一个可以共享的基类;
1.virtual class 抽象类,即可以被扩展但不能被直接实例化的类,它使用"virtual"关键词进行定义。
2.** pure virtual function** 纯虚方法,这是一种没有实体的方法原型。相当于一个声明,只能在抽象类中定义。

2.3 $cast

理解$cast 的作用,需要先给出父类,子类,父类句柄,子类句柄1,子类句柄2来进行说明。

birds father_handle;
parrot parrot_monday, parrot_tuesday;
father_handle = new(); // class 实例化需要构造函数,2.1 中未进行定义,应补充
parrot_monday = new();
parrot_tuesday = new();
//test 1
father_handle = parrot_monday
father_handle.print(); // success

//test 2
parrot_monday = father_handle;// failed 
parrot_monday.print();

// test 3
father_handle = parrot_monday
father_handle.print();
if($cast(parrot_tuesday, father_handle))// success, cast 转化成功,cast function只返回0 or 1, 需要自行加判断条件。
	begin ... end
else $display("cast error.");
// $function 的模板function int $cast(singular dest_var, singular source_exp);
***Summary for $cast.***

c a s t 在 s y s t e m v e

  • 1
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值