多态(polymorphism),从字面意思上看指的是多种形式,在oop(面向对象编程)中指的是同一个父类的函数可以体现为不同的行为。
在systemverilog中,指的是我们可以使用父类句柄来保存子类对象,并直接使用父类句柄来调用子类的方法。
因此,systemverilog中的多态性是一种根据父类对象句柄实际指向不同的对象类型使相同的代码具备不同行为的能力。
为了实现这个目的,父类中的function或者task必须是virtual的。这样,当扩展类覆盖这个function或者task时,实际指向扩展类的父类句柄就可以调用扩展类的function或者task
因此,用一句话可以概括:
polymorphism = inheritance + virtual methods + upcasting.
systemverilog多态的示例:
class vehicle; // parent class virtual function vehicle_type( ); // virtual function $display(vehicle); endfunction virtual task color( ); // virtual task $display(it has color); endtask endclass class four_wheeler extends vehicle; //child class function vehicle_type( ); //override parent class virtual //function $display(it is a four wheeler); endfunction task color( ); //override parent class virtual task $display(it has diferent colors); endtask endclass class benz extends four_wheeler; // grand child class function vehicle_type(); $display(it is a benz); endfunction task color(); $display(it is black); endtask endclass module polymorphism; initial begin vehicle vehcl; four_wheeler four_whlr; benz benz; four_whlr = new ( ); benz = new ( ); vehcl=four_whlr;//parent class variable holding child //class handle. no need to create an //object (of vehcl) by calling a new method vehcl.vehicle_type( ); // accessing child (four_whlr) class method from // parent class (vehcl) variable vehcl=benz; vehcl.vehicle_type( ); //accessing grand child method from parent class //variable (vehcl) four_whlr=benz; four_whlr.color( ); // accessing child class method from parent class // four_whlr end endmodule
仿真log:
it is a four wheelerit is a benzit is black$fnish at simulation time 0 v c s s i m u l a t i o n r e p o r t
在上面这个例子中,我们首先声明一个名为“vehicle”的父类。在这个父类中,我们声明了两个virtual 方法“vehicle_type”和“color.” 。
然后我们创建一个名为“four_wheeler”的扩展类,并覆盖父类的virtual方法。
我们再扩展自“four_wheeler”创建一个扩展类“benz”。
所以,类的继承关系如下:
“vehicle” -> “four_wheeler” ->“benz”
在program “polymorphism”中,我们声明每种class类型的句柄,但是仅实例化两个子类,即“four_whlr”和“benz”,而没有实例化父类。
这里需要回顾下一个upcast的概念,即将扩展类对象直接赋值给父类句柄。当我们实例化扩展类对象“four_wheeler”时,实际上为该类“four_wheeler”和相应的父类“vehicle”分配了物理内存空间。
当我们执行
vehcl = four_whlr
父类句柄中就包含了子类实例,据此我们可以访问被覆盖的virtual方法,这就是多态性。
下面是另外一个展示多态性的示例:
// base class class animals; virtual function void display( ); $display(inside base class animals); endfunction endclass // extended class 1 class parrot extends animals; function void display( ); $display(inside extended class parrot); endfunction endclass // extended class 2 class tiger extends animals; function void display( ); $display(inside extended class tiger); endfunction endclass // extended class 3 class lion extends animals; function void display( ); $display(inside extended class lion); endfunction endclass // module module polymorphism; initial begin //instantiate subclasses parrot p1 = new( ); lion a1 = new( ); tiger t1 = new( ); //base class array variable animals a1[3]; //assigning extended class object handles to //base class array variable a1[0] = p1; a1[1] = a1; a1[2] = t1; //accessing extended class methods using base class variable a1[0].display( ); a1[1].display( ); a1[2].display( ); end endmodule
仿真log:
inside extended class parrotinside extended class lioninside extended class tiger v c s s i m u l a t i o n r e p o r t
分别将不同的子类对象赋值被3个父类句柄,可以看出最后相同的父类函数打印出不同的内容。
WMZ-03型温度指示仪的工作原理及使用方法
氮化镓晶体管是如何提高开关效率的?
芯片nm越小越好吗 现在世界上芯片最小能到多少nm
GTC23 | 在线观看派对:“NVIDIA Omniverse 用户大会”精彩来袭!
Open AI进军芯片业技术突破与市场机遇
从本质上理解SystemVerilog的多态(Polymorphism)
采用FPGA实现多项式运算
伺服压力机与普通压力机的的区别是什么
可以覆盖手机等移动终端的国产操作系统鸿蒙2.0正式登场
重建资金困难,JDI只能出售工厂了
Maxim推出Teridian三相功率测量片上系统(SoC) 78M6631
应用于机器视觉的3D激光三角测量技术
锂电复合铜箔新突破,未来发展可期
如何避免电力系统中电能质量问题
电缆桥架的适用场合有哪些
石墨烯与硅簇革新锂电池 充电率提升10倍
DOC结构与工作原理
Play Labs和Theta Labs合作发展区块链、虚拟现实和AR领域
ADI公司的电流检测放大器实现业界最高精度,适合工业和汽车应
缺芯更缺电,日本汽车业是否为此做好了准备