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

本文深入探讨了面向对象编程中的数据隐藏与封装概念,通过对比local和protected关键字的使用,详细解释了成员变量的访问控制机制,包括它们如何限制外部访问以保护数据安全,以及在子类中的可见性和使用范围。

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

                        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.

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可以通過構造函數來賦值,並且可以通過子類的方法來修改父類的成員變量。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

元直数字电路验证

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

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

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

打赏作者

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

抵扣说明:

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

余额充值