在 SystemVerilog 中,我们可以使用 class 来进行面向对象编程。例如:

class Transaction;
  int count = 0;
  int id;

  function new();
    id = count++;
  endfunction

endclass

Transaction b1, b2;

initial begin
  b1 = new();
  b2 = new();
end

那么 b2.id1

解释:

  • Transaction 类定义了一个名为 count 的静态变量,用于跟踪创建的 Transaction 对象的数量。
  • new() 函数是 Transaction 类的构造函数,用于初始化新的 Transaction 对象。
  • 在构造函数中,id 属性被赋值为 count 的值,然后 count 自增。
  • 当创建第一个 Transaction 对象 b1 时,count 为 0,所以 b1.id 为 0。
  • 当创建第二个 Transaction 对象 b2 时,count 为 1,所以 b2.id 为 1。

因此,b2.id 的值为 1

SystemVerilog 面向对象编程:class Transaction 实例化

原文地址: https://www.cveoy.top/t/topic/mNzh 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录