1.静态变量
class packet;
int addr ;
int data ;
static int static_count;//定义静态变量
int count;
function new(int a,b);
addr = a;
data = b;
static_count ++; //静态变量自加
count ++;
$display("addr = %0d,data = %0d,static_count = %0d,count =%0d\n",addr,data,static_count,count);
endfunction
endclass
module tb;
packet p1;
packet p2;
packet p3;
//每一个实例都具有类变量的独立副本,而当类变量定义为static时
//该变量就是所有实例的唯一副本,可以理解为该变量具有记忆性
//所以打印时该变量会累加
initial begin
p1 = new(2,3);
p2 = new(2,3);
p3 = new(2,3);
end
endmodule
2.静态方法
1.静态方法可以不用实例化直接调用
class packet;
int addr ;
int data ;
static int static_count;
int count;
function new();
static_count ++;
endfunction
//定义一个静态方法,打印显示静态变量的值
static function display();
$display("static_count = %0d",static_count);
endfunction
endclass
module tb;
packet p1;
initial begin
//实例化后普通调用
p1 = new();
p1.display;
//静态方法可以不用实例化,用::符号直接调用
packet::display;
end
endmodule
2.静态方法里不能包含非静态变量
class packet;
int addr ;
int data ;
static int static_count;
int count;
function new();
static_count ++;
count = 10;
endfunction
static function display();
$display("static_count = %0d,count = %0d",static_count,count);
endfunction
endclass
3.typedef class
当类中包含一个尚未定义的类时,需要使用typedef 定义类
typedef packet; // 或者用 typedef class packet 来定义类
class mypacket;
packet p1; //创捷类的句柄,如果前面没有定义类,编译器会出错
function new();
p1 = new();
$display("data = %0d",p1.data);
endfunction
endclass
class packet;
int addr ;
int data ;
static int static_count;
int count;
function new();
data = 10;
endfunction
endclass
module tb;
mypacket p2;
initial begin
p2 = new();
end
endmodule
当未使用typedef定义时编译器会报错。