访问类型(用于修饰成员变量和方法)
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;
//endfunction
function 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
endclass
module 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();
end
endmodule : 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;
endfunction
function void init(int skin,int filling,int flavor ) ;
this.skin = skin;
this.filling = filling;
this.flavor = flavor;
endfunction
function 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
endclass
module 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;
endfunction
function void init(int skin,int filling,int flavor ) ;
this.skin = skin;
this.filling = filling;
this.flavor = flavor;
endfunction
function 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
endclass
class xiaolongbao extends baozi;
int feature;//新增成员变量
function new(string name,int weight,int feature);//构造函数
super.new(name,weight);//super调用父类的方法
this.feature=feature;//相比baozi多了一个方法
endfunction
function 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);
endfunction
endclass
module top;
xiaolongbao xiaolongbao_h;
initial begin
xiaolongbao_h = new("xiaolongbao",50,2);
xiaolongbao_h.make_xiaolongbao();
end
endmodule : 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...");
endfunction
endclass
class 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
endclass
module 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();//多态
end
endmodule : top
sim_report