以兩個例子說明參數化類別及聲明靜態變數時的情況:
eg:
program param_stack;
class stack #(type T = int);
int m_cnt;
static int counter = 2;
function new;
m_cnt = counter++;
endfunction: new
endclass: stack
class stacked extends stack #(real);
endclass: stacked
typedef stack #(byte) stack_byte;
typedef stack #() stact_int;
stack_byte S1 = new();
stack_byte S2 = new();
stack S3 = new();
stack #(bit) S4 = new();
stacked S5 = new();
initial begin
$display ("Counter value of S1 instance = %0d", stack #(byte)::counter);
$display ("Counter value of S2 instance = %0d", stack_byte:: counter);
$display ("Counter value of S3 instance = %0d", stack #()::counter);
$display ("Counter value of S4 instance = %0d", stack#(bit)::counter);
$display ("Counter value of S5 instance = %0d", stacked::counter);
end
endprogram: param_stack
列印的值依次為:
3
4
3
3
3
解釋:雖然靜態變數只會存在一個副本。
由於S1和S2均由stack_byte建立,所以S1時counter的值為3,S2為4;
S3則是由預設參數類建立,等同於程式中的stack_int,counter值為3;
S4則是type為bit的類建立,counter同樣為3;
S5亦然。
即當參數類的參數不同時,他們是不同的類。
註:
需要注意參數化類引用靜態變數的方法。