SystemVerilog中多态与虚方法
在验证过程中,往测试平台中添加新的测试激励很正常的事,这样的话就需要对原来的测试平台进行改进,有的时候需要修改原来的代码甚至可能修改一些数据结构,这个过程中可能会导致在原来的验证平台中引入意外的错误。那么为了保证原有平台或数据结构不变,通过对已经有的基类进行引申或者扩展,从而完善整个验证平台。
从基类做扩展并产生新的子类的过程叫类的派生,当一个类被扩展并创建之后,该派生类就继承了其基类的数据成员、属性和方法,这就是类的继承。
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
# 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.
上述例子中利用了虚方法,如果去掉虚方法,那么情况如下:
ex2:
还是上述代码,只是去掉virtual,则仿真结果如下:
# 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:
如果父类添加virtual method,但是子类不添加,仿真结果如下:
# 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:
基类添加virtual method 后面的所有继承类都不添加,代码如下:
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_vir class 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 end endprogram
# 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.
欢迎点赞,关注,分享~~, 本文原发于微信公众号【 数字ic小站 】