`
ribishuangba
  • 浏览: 292615 次
文章分类
社区版块
存档分类
最新评论

c#.net中类的覆写(OverRide)

 
阅读更多

c#.net中类的覆写(OverRide)

public class MyBase
... {
public virtual string Meth1()
... {
return " MyBase-Meth1 " ;
}

public virtual string Meth2()
... {
return " MyBase-Meth2 " ;
}

public virtual string Meth3()
... {
return " MyBase-Meth3 " ;
}

}


class MyDerived:MyBase
... {
// OverridesthevirtualmethodMeth1usingtheoverridekeyword:
public override string Meth1()
... {
return " MyDerived-Meth1 " ;
}

// ExplicitlyhidethevirtualmethodMeth2usingthenew
// keyword:
public new string Meth2()
... {
return " MyDerived-Meth2 " ;
}

// Becausenokeywordisspecifiedinthefollowingdeclaration
// awarningwillbeissuedtoalerttheprogrammerthat
// themethodhidestheinheritedmemberMyBase.Meth3():
public string Meth3()
... {
return " MyDerived-Meth3 " ;
}


public static void Main()
... {
MyDerivedmD
= new MyDerived();
MyBasemB
= (MyBase)mD;

System.Console.WriteLine(mB.Meth1());
System.Console.WriteLine(mB.Meth2());
System.Console.WriteLine(mB.Meth3());
}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics