广东快乐十分

chenyangbinga的个人空间 http://blog.njhwhs.com/chenyb [收藏] [复制] [RSS]

日志

SystemVerilog中多态与虚方法

已有 491 次阅读2019-12-27 12:12 |个人分类:SystemVerilog|系统分类:芯片设计| 验证

本文从微信公众号--数字IC小站,​转载,欢迎关注,微信公众号更新更多更快

在验证过程中,往测试平台中添加新的测试激励很正常的事,这样的话就需要对原来的测试平台进行改进,有的时候需要修改原来的代码甚至可能修改一些数据结构,这个过程中可能会导致在原来的验证平台中引入意外的错误。那么为了保证原有平台或数据结构不变,通过对已经有的基类进行引申或者扩展,从而完善整个验证平台。
从基类做扩展并产生新的子类的过程叫类的派生,当一个类被扩展并创建之后,该派生类就继承了其基类的数据成员、属性和方法,这就是类的继承。
继承后的类可以实现以下功能:
1.继承了原来类中的方法,并可以修改
2.添加新的方法
3.添加新的数据成员
在实现以上功能的同时需要满足一定的规则:
1.子类继承父类的所有数据成员和方法
2.子类可以添加新的数据成员和方法
3.子类可以重写父类中的数据成员和方法
4.如果一个方法被重写,其必须保持和父类中原有定义有一致的参数
5.子类可以通过super操作符来引用父类中的方法和成员
6.被声明为local的数据成员和方法只能对自己可见,对外部和子类都不可见;对声明为protected的数据成员和方法,对外部不可见,对自身和子类可见。

首先看下:

ex1:

class A; 
 virtual function void print_name();
$display ("this is the class a.\n"); 
endfunction
endclass:A
class B extends A; 
virtual function void print_name(); 
$display ("this is the class b.\n"); 
endfunction
virtual function void new_display(); 
$display("this a new method.\n"); 
endfunction
endclass:B
program testbench;
initial begin 
begin:class_test1 
A classa; 
B classb; 
classa = new(); 
classb = new();
classa.print_name(); 
classb.print_name(); 
end 
begin:class_test2 
A classa; 
B classb; 
classb = new(); 
classa = classb; 
classa.print_name(); 
classb.print_name(); 
end 
begin:class_test3 
A classa1,classa2; 
B classb1,classb2; 
classa2 = new(); 
classb1 = new(); 
classa1 = classb1; 
classb2 = classb1; 
classa2.print_name(); 
classb2.print_name(); 
classa2 = classa1; 
$cast(classb2,classa2); 
classa2.print_name(); 
classb2.new_display(); 
classb2.print_name();          
//  classa2.new_display(); 
end 
end
endprogram

SHANGSHUDAIMAZHONG,HENRONGYILIJIE,YINWEIclassa2ZHONGBUHANYOUnew_displayZHEIZHONGmethod,YINCIHUIBAOerror,WOBAQIZHUSHIDIAOLE,QICI,WOMENSHIYONG$castZHUANHUANDEQIANTISHIXIANBAZILEIFUJIFULEI,ZHEISHIHOUCAINENGSHIYONGcastBAGAIFULEIZAIFUJIZILEI,RUGUOMEIYOUQIANTITIAOJIAN,HAISHIHUIBAOerrorDE,FANGZHENJIEGUORUXIA:

# this is the class a.
# this is the class b.
# this is the class b.
# this is the class b.
# this is the class a.
# this is the class b.
# this is the class b.
# this a new method.
# this is the class b.

广东快乐十分SHANGSHULIZIZHONGLIYONGLEXUFANGFA,RUGUOQUDIAOXUFANGFA,NEIMEQINGKUANGRUXIA:

ex2:

广东快乐十分HAISHISHANGSHUDAIMA,ZHISHIQUDIAOvirtual,ZEFANGZHENJIEGUORUXIA:

# this is the class a.
# this is the class b.
# this is the class a.
# this is the class b.
# this is the class a.
# this is the class b.
# this is the class a.
# this a new method.
# this is the class b.
#

ex3:

广东快乐十分RUGUOFULEITIANJIAvirtual method,DANSHIZILEIBUTIANJIA,FANGZHENJIEGUORUXIA:

# this is the class a.
# this is the class b.
# this is the class b.
# this is the class b.
# this is the class a.
# this is the class b.
# this is the class b.
# this a new method.
# this is the class b.
#

ex4:

广东快乐十分JILEITIANJIAvirtual method HOUMIANDESUOYOUJICHENGLEIDOUBUTIANJIA,DAIMARUXIA:

class A; 
 virtual function void print_name(); 
$display ("this is the class a.\n"); 
endfunction
endclass:A
class B_novir_vir extends A; 
function void print_name(); 
$display ("this is the class b.\n"); 
endfunction 
function void new_display(); 
$display("this a new method.\n"); 
endfunction
endclass:B_novir_virclass 
C_novir_vir extends B_novir_vir; 
function void print_name(); 
$display ("this is the class C.\n"); 
endfunction
endclass:C_novir_vir
program testbench3; 
initial begin 
begin:class_test1 
C_novir_vir classc; 
B_novir_vir classb; 
classb = new(); 
classc = new();
classb.print_name(); 
classc.print_name(); 
end begin:class_test2
C_novir_vir classc; 
B_novir_vir classb; 
classc = new();
classb = classc; 
classb.print_name(); 
classc.print_name();
end begin:class_test3 
C_novir_vir classc1,classc2;
B_novir_vir classb1,classb2; 
classb2 = new(); 
classc1 = new(); 
classb1 = classc1; 
classb2.print_name();
classc1.print_name();
classb2 = classb1; 
$cast(classc2,classb2); 
classb2.print_name(); 
classc2.new_display();
classc2.print_name();
end 
endendprogram

FANGZHENJIEGUORUXIA,SHUOMINGZHIYAOSHIJILEIYOUvirtual methodJIUXING,QITAZILEIKEYIBUXUYAOYOU。

# this is the class b.
#
# this is the class C.
#
# this is the class C.
# this is the class C.
# this is the class b.
# this is the class C.
# this is the class C.
# this a new method.
# this is the class C.


评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 注册

关闭

站长推荐上一条 /2 下一条

小黑屋| 关于我们| 联系我们| 在线咨询 |  EETOP 创芯网 ( )

GMT+8, 2020-9-25 18:43 , Processed in 0.034213 second(s), 8 queries , Gzip On, Redis On.

Powered by X3.4

广东快乐十分© 2001-2017

返回顶部
var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?be055eb5acfdc25d3bd72279b8418725"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })();