using System;
class BaseClass
{
public virtual void baseInfo()
{
Console.WriteLine("基本クラス");
}
}
class DerivedClass1 : BaseClass
{
public override void baseInfo()
{
Console.WriteLine("派生クラス1");
}
}
class DerivedClass2 : BaseClass
{
new public void baseInfo()
{
Console.WriteLine("派生クラス2");
}
}
class MainClass
{
static void Main()
{
BaseClass bc = new DerivedClass1();
DerivedClass1 dc1 = bc as DerivedClass1;
DerivedClass2 dc2 = bc as DerivedClass2;
string s = "ダウンキャストできない";
if (dc1 != null)
{
dc1.baseInfo();
}
else
{
Console.WriteLine(s);
}
if (dc2 != null)
{
dc2.baseInfo();
}
else
{
Console.WriteLine(s);
}
}
}
0 コメント:
コメントを投稿