virtual class animal; protected int age=-1; protected string name; function new(int a, string n); age = a; name = n; endfunction : new function int get_age(); return age; endfunction : get_age function string get_name(); return name; endfunction : get_name pure virtual function void make_sound(); endclass : animal class lion extends animal; protected string name; function new(int age, string n); super.new(age, n); endfunction : new function void make_sound(); $display ("The lion, %s, says Roar", get_name()); endfunction : make_sound endclass : lion class chicken extends animal; function new(int age, string n); super.new(age, n); endfunction : new function void make_sound(); $display ("The Chicken, %s, says BECAWW", get_name()); endfunction : make_sound endclass : chicken //animal_cage的代码跟lion_cage基本一样,除了前者带有一个参数。
//在第一行可以看见我们定义的type参数T,然后我们用这个参数来定义队列的类型,并用作cage_animal的参数类型
class animal_cage #(type T); protected static T cage[$]; //用参数来定义队列的类型 static function void cage_animal(T l); //将参数用作cage_animal的参数类型 cage.push_back(l); endfunction : cage_animal static function void list_animals(); $display("Animals in cage:"); foreach (cage[i]) $display(cage[i].get_name()); endfunction : list_animals endclass : animal_cage module top; initial begin lion lion_h; chicken chicken_h; lion_h = new(15, "Mustafa"); animal_cage #(lion)::cage_animal(lion_h); //用lion类作为参数来访问cage_animal静态方法 lion_h = new(15, "Simba"); //释放 animal_cage #(lion)::cage_animal(lion_h); chicken_h = new(1, "Clucker"); animal_cage #(chicken)::cage_animal(chicken_h); //用chicken类作为参数来访问cage_animal静态方法 chicken_h = new(1, "Scratchy"); animal_cage #(chicken)::cage_animal(chicken_h); $display("-- Lions --"); animal_cage #(lion)::list_animals(); $display("-- Chickens --"); animal_cage #(chicken)::list_animals(); end endmodule : top
每当例化一个参数化类,你用不同的参数创建的类都是不同的
下面的例子中,我们不用静态方法来访问动物笼子了,我们要开始用例化的动物笼子对象来存放其他的动物
virtual class animal; protected int age=-1; protected string name; function new(int a, string n); age = a; name = n; endfunction : new function int get_age(); return age; endfunction : get_age function string get_name(); return name; endfunction : get_name pure virtual function void make_sound(); endclass : animal class lion extends animal; protected string name; function new(int age, string n); super.new(age, n); endfunction : new function void make_sound(); $display ("The lion, %s, says Roar", get_name()); endfunction : make_sound endclass : lion class chicken extends animal; function new(int age, string n); super.new(age, n); endfunction : new function void make_sound(); $display ("The Chicken, %s, says BECAWW", get_name()); endfunction : make_sound endclass : chicken class animal_cage #(type T); protected T cage[$]; function void cage_animal(T l); //注意此处,去掉了static cage.push_back(l); endfunction : cage_animal function void list_animals(); //注意此处,去掉static $display("Animals in cage:"); foreach (cage[i]) $display(cage[i].get_name()); endfunction : list_animals endclass : animal_cage module top; lion lion_h; chicken chicken_h; animal_cage #(lion) lion_cage; animal_cage #(chicken) chicken_cage; initial begin lion_cage = new(); //例化一个新的狮子笼对象 lion_h = new(15, "Mustafa"); lion_cage.cage_animal(lion_h); lion_h = new(15, "Simba"); lion_cage.cage_animal(lion_h); chicken_cage = new(); //例化新的chicken_cage chicken_h = new(1, "Little Red Hen"); chicken_cage.cage_animal(chicken_h); chicken_h = new(1, "Lady Clucksalot"); chicken_cage.cage_animal(chicken_h); $display("-- Lions --"); lion_cage.list_animals(); $display("-- Chickens --"); chicken_cage.list_animals(); end endmodule : top