[SV]Systemverilog數據的封裝和影藏(Data Hiding and Encapsulation)

                        Data hiding and Encapsulation

       The technique of hiding the data within the class and making it available only through the methods, is known as encapsulation.Because it seals the data (and internal methods) safely inside the “capsule” of the class, where it can be accessed only by trusted users  (i.e., by the methods of the class).

一、Access Control

       By default all the members and methods of a class are accessible from anywhere using the object handle, sometimes this could corrupt the class members values, which should not be touched at all.
       Access control rules that restrict the members of a class from being used outside the class, this is achieved by prefixing the class members with the keywords,

  • local
  • protected

二、local class members

       External access to the class members can be avoided by declaring members as local.Any violation could result in a compilation error.

(1)SYNTAX:

local integer x;

(2)Local Class members examples

       Not:ACCESSING LOCAL VARIABLE OUTSIDE THE CLASS ( NOT ALLOWED )

       In below example,The local variable declared inside the class is trying to access from outside the class by using object handle.As the variable is declared as local, which leads to a compilation error.

class parent_class;
  local bit [31:0] tmp_addr;
   
  function new(bit [31:0] r_addr);
    tmp_addr = r_addr + 10;
  endfunction
 
  function display();
    $display("tmp_addr = %0d",tmp_addr);
  endfunction
endclass
 
 
//   module
module encapsulation;
  initial begin
    parent_class p_c = new(5);
        
    p_c.tmp_addr = 20;  //Accessing local variable outside the class
    p_c.display();
  end
endmodule

  Simulator Output

Error- Illegal class variable access
testbench.sv,
Local member 'tmp_addr' of class 'parent_class' is not visible to scope
'encapsulation'.

       Not:ACCESSING LOCAL VARIABLE WITHIN THE CLASS ( ALLOWED )

       In the below example, The local variable declared inside the class is being accessed inside the class. as it is allowed, no compilation error is observed.

class parent_class;
  local bit [31:0] tmp_addr;
   
  function new(bit [31:0] r_addr);
    tmp_addr = r_addr + 10;
  endfunction
 
  function display();
    $display("tmp_addr = %0d",tmp_addr);
  endfunction
endclass
 
//   module
module encapsulation;
  initial begin
    parent_class p_c = new(5);
    p_c.display();
  end
endmodule

  Simulator Output:

Addr = 15

       Not:父類中用local修飾的成員變量,在子類中是不可見的

class parent_class;
  local bit [31:0] tmp_addr;
   
  function new(bit [31:0] r_addr);
    tmp_addr = r_addr + 10;
  endfunction
 
  function display();
    $display("tmp_addr = %0d",tmp_addr);
  endfunction
endclass

class child_class extends parent_class;
   
  function new(bit [31:0] r_addr);
    tmp_addr = r_addr + 10;
  endfunction

  function addr_inc();
    tmp_add += 10;
  endfunction : addr_inc
 
  function display();
    $display("tmp_addr = %0d",tmp_addr);
  endfunction
endclass : child_class
 
//   module
module encapsulation;
  initial begin
    parent_class p_c = new(5);
    p_c.display();
    child_class  c_c = new(10);
    c_c.addr_inc();
    c_c.display();    
  end
endmodule

  Simulator Output:

Error-[SV-ICVA] Illegal class variable access testbench.sv,
Local member 'tmp_addr' of class 'parent_class' is not visible to scope 'child_class'

 

三、Protected class members

       In some use cases, it is required to access the class members only by the derived class’s, this can be done by prefixing the class members with the protected keyword.Any violation could result in a compilation error.

(1)SYNTAX:

protected integer x;

(2)Protected Class members examples

       ACCESSING A PROTECTED VARIABLE OUTSIDE THE CLASS ( NOT ALLOWED )

       In the below example, The protected variable declared inside the class is trying to access from outside the class by using object handle.As the variable is declared as protected, which leads to a compilation error.

扫描二维码关注公众号,回复: 10466865 查看本文章
class parent_class;
  protected bit [31:0] tmp_addr;
   
  function new(bit [31:0] r_addr);
    tmp_addr = r_addr + 10;
  endfunction
 
  function display();
    $display("tmp_addr = %0d",tmp_addr);
  endfunction
endclass
 
class child_class extends parent_class;
  function new(bit [31:0] r_addr);
    super.new(r_addr);
  endfunction
   
  function void incr_addr();
    tmp_addr++;
  endfunction 
endclass
 
//   module
module encapsulation;
  initial begin
    parent_class p_c = new(5);
    child_class  c_c = new(10);
         
    // variable declared as protected cannot be accessed outside the class
    p_c.tmp_addr = 10;
    p_c.display();
    
    c_c.incr_addr();  //Accessing protected variable in extended class
    c_c.display();
  end
endmodule

  Simulator Output

Error- Illegal class variable access
testbench.sv,
Protected member 'tmp_addr' of class 'parent_class' is not visible to scope
'encapsulation'.

       ACCESSING A PROTECTED VARIABLE IN THE EXTENDED CLASS ( ALLOWED )

       In the below example, The protected variable declared inside the class is being accessed inside the extended class. as it is allowed, no compilation error is observed.

class parent_class;
  protected bit [31:0] tmp_addr;
   
  function new(bit [31:0] r_addr);
    tmp_addr = r_addr + 10;
  endfunction
 
  function display();
    $display("tmp_addr = %0d",tmp_addr);
  endfunction
endclass
 
class child_class extends parent_class;
  function new(bit [31:0] r_addr);
    super.new(r_addr);
  endfunction
   
  function void incr_addr();
    tmp_addr++;
  endfunction 
endclass
 
//   module
module encapsulation;
  initial begin
    child_class  c_c = new(10);
     
    c_c.incr_addr();  //Accessing protected variable in extended class
    c_c.display();
  end
endmodule

  Simulator Output

Addr = 21

四、結論:

       Local的限制性更大,Local的變量只能通過構造函數來賦值,並且子類的方法不能更改父類的成員變量的值。

       protected可以通過構造函數來賦值,並且可以通過子類的方法來修改父類的成員變量。

发布了185 篇原创文章 · 获赞 118 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/gsjthxy/article/details/105204367