using System;
class BaseClass
{
public virtual void printOut()
{
Console.WriteLine("BaseClass");
}
}
class DerivedClass1 : BaseClass
{
public override void printOut()
{
Console.WriteLine("DerivedClass1");
}
}
class DerivedClass2:BaseClass
{
public override void printOut()
{
Console.WriteLine("DerivedClass2");
}
}
class MainClass
{
static void Main()
{
/* DerivedClass1型をBaseClass型に
* Upcast */
BaseClass sample1=new DerivedClass1();
// 出力値:DerivedClass1
sample1.printOut();
/* BaseClass型をDerived1Class1型に
* DownCast */
DerivedClass1 sample2
= (DerivedClass1)sample1;
/* 元がDerivedClass1型なので
* DerivedClass2型にDowncast
* しようとするとエラーになる
DerivedClass2 sample3
= (DerivedClass2)sample1; */
}
}
0 コメント:
コメントを投稿