嵌入式技术
protected类属性或方法具有local成员的所有特征,除此之外的是,protected类属性或方法对扩展类是可见的。
也就是说,protected类属性或方法可以被类自身和扩展类访问,依然不允许在类的外部访问。
声明一个protected类属性或方法,需要通过关键字“protected”。
class packet; protected int addr; protected function void disp(input int data); $display("From packet"); $display(" data = %h", data); $display(" addr = %h", addr); endfunction endclass class eth_packet extends packet; function set_addr; addr = 'hf; //protected property 'addr' visible to extended class endfunction function void eth_disp; super.disp('hf); //protected method 'disp' visible to extended class endfunction function void disp(input int data); //OK to override 'protected' method in extended class $display("From eth_packet"); $display(" data = %h", data); endfunction endclass module class_TOP( ); initial begin packet p1; eth_packet e1; e1 = new( ); p1 = new( ); //p1.disp(20); //COMPILE ERROR //protected methods 'disp' not visible outside the class e1.eth_disp; e1.disp('hffff); end endmodule
仿真log:
From packet data = 000000ff addr = 00000000 From eth_packet data = 0000ffff V C S S i m u l a t i o n R e p o r t
在上面的例子中,我们声明了一个类“packet”,其中包含protected成员“addr”和一个protected方法“disp” 。
在扩展类“eth_packet”中,我们直接访问成员“addr”以及调用父类的“disp”方法都不会发生编译错误。
在module中直接调用“p1.disp(20);”就会发生编译错误:
Error-[SV-ICMA] Illegal class method access testbench.sv, 39 Protected method 'disp' of class 'packet' is not visible to scope 'class_TOP'. Please make sure that the above method is called only from its own class properties as it is declared as protected.
全部0条评论
快来发表一下你的评论吧 !