System Verilog_OOP(3)

文章介绍了面向对象编程的关键概念,包括封装(使用local限制成员变量的访问)、继承(子类扩展父类的功能)和多态(子类覆盖父类的虚函数)。通过具体的类和方法示例,阐述了如何在类设计中应用这些原则来提高代码的组织性和灵活性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

​访问类型(用于修饰成员变量和方法)

pubilc:如果没有指明访问类型,那么成员的默认类型是public,子类和外部均可以访问成员。

local:只有该类可以访问成员,子类和外部均无法访问。

protected:只有该类或者子类可以访问成员,而外部无法访问。

使用建议:但对于初学者以及应用范围较窄的验证环境,可以使用默认的访问类型,以便于在类的内部或者外部更方便地修改成员变量或者调用成员方法。


一、OOP封装

封装(Encapsulation):以事务(transation)为单位,将类的属性和方法封装在一起。

前面我们可以通过直接引用的方式对类的数据成员进行赋值

此时类中的数据成员对于外部来说都是可见的,相当于全局变量,这是比较危险的,因为不小心改变该类的内部数据成员变量的值可能会带来意想不到的问题。

因此,可以在声明该数据成员时前面加上关键字local

此时,变量将只能在类的内部由类的方法进行访问,在类外部使用之前直接引用的方式进行访问会提示出错。

class baozi;    string name;    int weight;    local int skin;    local int filling;    local int flavor;function new(string name,int weight);    this.name   = name ;    this.weight = weight;endfunction//function void init(int skin,int filling,int flavor ) ;//    this.skin = skin;//    this.filling = filling;//    this.flavor = flavor;//endfunctionfunction void make_baozi();    $display("A baozi is completed!");    $display("Baozi name is %s",name);    $display("Baoziweight is %d" ,weight);    $display("Baoziskin is %d",skin);    $display("Baozifilling is %d",filling) ;    $display("Baozi flavor is %d",flavor);endfunction endclassmodule top;    baozi baozi_h;      initial begin      baozi_h=new(.name("caibaozi") ,.weight(150));      baozi_h.skin = 0;      baozi_h.filling = 1;      baozi_h.flavor = 0;//      baozi_h.init(0,1,0);      baozi_h.make_baozi();    endendmodule : top

sim_report

由于不能通过直接引用的方式进行访问了,因此需要在类的内部定义一个方法来进行访问。

lass baozi;    string name;    int weight;    local int skin;    local int filling;    local int flavor;function new(string name,int weight);    this.name   = name ;    this.weight = weight;endfunctionfunction void init(int skin,int filling,int flavor ) ;    this.skin = skin;    this.filling = filling;    this.flavor = flavor;endfunctionfunction void make_baozi();    $display("A baozi is completed!");    $display("Baozi name is %s",name);    $display("Baoziweight is %d" ,weight);    $display("Baoziskin is %d",skin);    $display("Baozifilling is %d",filling) ;    $display("Baozi flavor is %d",flavor);endfunction endclassmodule top;    baozi baozi_h;      initial begin      baozi_h=new(.name("caibaozi") ,.weight(150));      baozi_h.init(0,1,0);      baozi_h.make_baozi();    end               endmodule : top

sim_report

成员方法也可以定义成loacl


二、OOP继承

继承(Inheritance):子类具有父类的属性和方法或者重新定义、追加属性和方法等。

class baozi;    string name;    int weight;   // local int skin;   // local int filling;   // local int flavor;    protected int skin;    protected int filling;    protected int flavor;function new(string name,int weight);    this.name   = name ;    this.weight = weight;endfunctionfunction void init(int skin,int filling,int flavor ) ;    this.skin = skin;    this.filling = filling;    this.flavor = flavor;endfunctionfunction void make_baozi();    $display("A baozi is completed!");    $display("Baozi name is %s",name);    $display("Baoziweight is %d" ,weight);    $display("Baoziskin is %d",skin);    $display("Baozifilling is %d",filling) ;    $display("Baozi flavor is %d",flavor);endfunctionendclassclass xiaolongbao extends baozi;    int feature;//新增成员变量function new(string name,int weight,int feature);//构造函数    super.new(name,weight);//super调用父类的方法    this.feature=feature;//相比baozi多了一个方法endfunctionfunction void make_xiaolongbao();    $display("A xiaolongbao is completed!");    $display("xiaolongbao name is %s",name) ;    $display("xiaolongbao weight is %d",weight);    $display("xiaolongbaoskin is %d",skin);    $display("xiaolongbaofilling is %d",filling);    $display("xiaolongbao ozi flavor is %d",flavor);    $display("xiaolongbao ozi feature is %d",feature);endfunctionendclassmodule top;    xiaolongbao xiaolongbao_h;    initial begin    xiaolongbao_h = new("xiaolongbao",50,2);    xiaolongbao_h.make_xiaolongbao();    endendmodule : top

sim_report


三、OOP多态

多态(Polymorphism):同一个父类的函数可以体现为不同的行为。

使用关键字virtual将父类中的方法定义成虚函数,当子类句柄赋给父类句柄,子类的方法能覆盖父类

class baozi;    string name;    int weight;    protected int skin;    protected int filling;    protected int flavor;    function new(string name,int weight);        this.name = name;        this.weight = weight;    endfunction    virtual function void eat();            $display("Put it straight in your mouth!");    endfunction        function void make_baozi();        $display("A baozi is completed!");        $display("Baozi name is %s",name);        $display("Baozi weight is %d",weight);        $display("Baozi skin is %d",skin);        $display("Baozi filling is %d",filling);        $display("Baozi flavor is %d",flavor);    endfunction    function void init(int skin,int filling,int flavor);        this.skin = skin;        this.filling = filling;        this.flavor = flavor;    endfunction    local function void B();        $display("do something...");    endfunctionendclassclass xiaolongbao extends baozi;    int feature;        function new(string name,int weight,int feature);        super.new(name,weight);        this.feature=feature;    endfunction     virtual function void eat();        $display("Dip it in vinegar and put it in your mouth!");    endfunction           function void make_xiaolongbao();        $display("A xiaolongbao is completed!");        $display("Xiaolongbao name is %s", name);        $display("Xiaolongbao  weight is %d", weight);        $display("Xiaolongbao  skin is %d", skin);        $display("Xiaolongbao  filling is %d", filling);        $display("Xiaolongbao ozi flavor is %d", flavor);        $display("Xiaolongbao ozi feature is %d", feature);    endfunction    endclassmodule top;      baozi    baozi_h;   xiaolongbao xiaolongbao_h;   initial begin      baozi_h = new(.name("caibaozi"),.weight(150));      baozi_h.init(0,1,0);      baozi_h.make_baozi();      xiaolongbao_h = new("xiaolongbao",50,2);      xiaolongbao_h.make_xiaolongbao();      baozi_h = xiaolongbao_h;//子类句柄赋给父类      baozi_h.eat();//多态   endendmodule : top

sim_report

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宝哥学IC

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值