[英]Calling base class functions in C#.net
public class c2
{
public void Print()
{
MessageBox.Show("C2");
}//print
}//c2
public class c1 : c2
{
public void Print()
{
MessageBox.Show("C1");
}//print
}//c1
How do i call Print() of base class(c2) using derived class Object(Function name are same in both classes) 我如何使用派生类Object调用基类(c2)的Print()(两个类中的函数名称均相同)
您可以使用base.Print()
来调用它。
If you have an instance of c1
, you can cast it to c2
to be able to call that method. 如果您有
c1
的实例,则可以将其c2
转换为c2
以便能够调用该方法。 For example: 例如:
var myc1 = new c1();
((c2)myc1).Print();
NOTE: This solution works in this example because c1.Print() only hides c2.Print() as opposed to overriding it. 注意:此解决方案在此示例中有效,因为c1.Print()仅隐藏 c2.Print()而不是覆盖它。
public class c2
{
virtual public void Print()
{
MessageBox.Show("C2");
}//print
}//c2
public class c1 : c2
{
override public void Print()
{
MessageBox.Show("C1");
base.Print();
}//print
}//c1
if you want run at C1's Print C2'S function, too. 如果您也想在C1的Print C2'S功能下运行。 But to override the function the print function you shall declared it abstract or virtual.
但是要覆盖该功能,您必须将其声明为抽象或虚拟。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.