基本定义
都是定义在类中,用来做内部变量使用的。
local和protected的区别
(1)local就是本地变量,那么在子类中就不能调用,外部不可调用。
(2)protected就是保护变量,子类可以调用操作,但是外部代码仍然不能操作该变量。
具体看如下实例,修改自路桑课件上的coding。
代码演示与结果仿真
这是将错误注释掉的,可以将注释去掉,观察仿真器的报错。如下都进行了展示。
module class_stu();
class cat;
protected string color;
local bit is_good;
function set_good(bit s);
this.is_good = s;
endfunction
endclass
//声明黑猫类/
class black_cat extends cat;
function new();
this.color = "BLACK";
//this.is_good = 0;
endfunction
function display();
$display("black_cat color is %s",this.color);
endfunction
endclass
//声明白猫类/
class white_cat extends cat;
function new();
this.color = "WHITE";
//$display("white_cat color is %s ,this cat is good %0d",this.color,this.is_good);//对于local变量,在子类的访问也是非法的
endfunction
function display();
//$display("white_cat color is %s ,this cat is good %0d",this.color,this.is_good);
$display("white_cat color is %s",this.color);
endfunction
endclass
initial begin
black_cat bk;
white_cat wt;
bk = new(); //初始化黑猫对象
wt = new(); //初始化白猫对象
//$display("black_cat color is %s",bk.color);//对于protect变量外部的访问,是不允许的
//bk.color="red";//对于protect变量的操作,属于外部调用,会报错
bk.display;
wt.display;
end
endmodule
删除注释后的报错。错误提示是对于local以及protect变量的不正确访问。