以下代码中的派生类Test2和Test3不是虚拟类型,那么为什么要覆盖这些函数以及如何阻止它发生? P.S - >我是新手,所以这个问题可能是愚蠢的抱歉。
public class Service : BaseEntity
{
public int ServiceID { get; set; }
public int RequestID { get; set; }
public string BusinessLineCode { get; set; }
public string BusinessLine { get; set; }
public bool IsPrimaryBusinessLine { get; set; }
public int ContractLineSLAID { get; set; }
}
答案 0 :(得分:1)
Test2::print
为virtual
,因此在指向print
的指针上调用Test2
或从Test2
派生的类指针将使用对象的运行时类型,用于确定要调用的函数。在您的情况下,对象的运行时类型为Test4
,因此它将调用Test4::print
。这就是virtual
函数的重点。
如果您想明确调用print
的其他版本,则可以执行此操作,例如test3->Test3::print();