今天我们反思,然后总结。
一 先看代码
`timescale 1ns/1ps
module tb_top;
class Base;
function void print(int a);
$display("Base: int = %0d", a);
endfunction
endclass
class Sub extends Base;
function void print(string s);
$display("Sub: string = %s", s);
endfunction
endclass
initial begin
Sub s = new;
s.print("hello"); // 合法,输出 "Sub: string = hello"
s.print(10); // 可能报出编译错误!找不到匹配的print(int),也可能不错报
end
endmodule
在SystemVerilog中,当子类定义了一个与基类同名的方法(即使参数列表不同),基类的同名方法会被隐藏。
这是因为SystemVerilog的作用域规则规定:子类中的方法名会覆盖基类中的所有同名方法,无论参数是否匹配。<