class rectangle;
int length;
int width;
function new(int l, int w);
length = l;
width = w;
endfunction
function int area();
return length * width;
endfunction
endclass
class square extends rectangle;
function new(int side); //我们重载了new()构造函数,这个square版的new()只有一个参数 : 边长。
super.new(.l(side), .w(side)); //super关键字指示编译器来显式的引用父类中定义的数据成员和方法。
endfunction
endclass
module top_class ;
rectangle rectangle_h;
square square_h;
initial begin
rectangle_h = new(.l(50),.w(20));
$display("rectangle area: %0d", rectangle_h.area());
square_h = new(.side(50));
$display("square area: %0d", square_h.area());
//我们声明了square的一个变量然后用 new() 来创建对象。我们调用了new()两次,但是两次运行的是不同的版本 : 一个是rectangle的一个是 square的。编译器知道所有 new() 的调用跟表达式左边的变量类型都是相关的。
end
endmodule