C#入门基础——基类的引用实例
namespace 基类的引用实例
{
class Program
{
static void Main(string[] args)
{
MyDerivedClass derived = new MyDerivedClass();//派生类的实例化.
MyBaseClass mybc= derived;//转换成基类
//MyBaseClass mybcc = new MyBaseClass();
derived.Print(); //从派生类部分调用Print.
mybc.Print(); //从基类部分调用Print.
//mybcc.Print();
Console.ReadKey();
}
}
class MyBaseClass
{
public void Print()
{
Console.WriteLine("This is the base class.");
}
}
class MyDerivedClass : MyBaseClass
{
new public void Print()
{
Console.WriteLine("This is the derived class.");
}
}
}
输出结果:
This is the derived class.
This is the base class. {:7_146:}
页:
[1]