python3重写new方法_SystemVerilog中多态与虚方法

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

SystemVerilog中多态与虚方法​mp.weixin.qq.com
087ef08ee6f8348766a727f571d278fc.png

在验证过程中,往测试平台中添加新的测试激励很正常的事,这样的话就需要对原来的测试平台进行改进,有的时候需要修改原来的代码甚至可能修改一些数据结构,这个过程中可能会导致在原来的验证平台中引入意外的错误。那么为了保证原有平台或数据结构不变,通过对已经有的基类进行引申或者扩展,从而完善整个验证平台。
从基类做扩展并产生新的子类的过程叫类的派生,当一个类被扩展并创建之后,该派生类就继承了其基类的数据成员、属性和方法,这就是类的继承。
继承后的类可以实现以下功能:
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

上述代码中,很容易理解,因为classa2中不含有new_display这种method,因此会报error,我把其注释掉了,其次,我们使用$cast转换的前提是先把子类赋给父类,这时候才能使用cast把该父类再赋给子类,如果没有前提条件,还是会报error的,仿真结果如下:

# 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

仿真结果如下,说明只要是基类有virtual method就行,其他子类可以不需要有。

# 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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值