Re: [问题] 父类别转子类别

楼主: bantime (景)   2015-11-24 00:20:22
好吧...我知道这很蠢..
public class father
{
public void FatherFunction()
{
Console.WriteLine("this is father");
}
}
public class child : father
{
public void ChileFunction()
{
Console.WriteLine("this is child");
}
}
List<father> fatherCol = new List<father>()
{
new father(), new child()...brabra...
};
foreach(var tar in fatherCol)
{
dynamic d = tar;
switch(tar.GetType().Name)
{
case "father":
d.FatherFunction();
break;
case "child":
d.ChileFunction();
break;
}
}
※ 引述《grace0523 (小小)》之铭言:
: Class father
: {}
: Class child : father
: {}
: father f = new child();
: Child C1 = (child)f ;//第一种强转
: child C2 = f as child ;//第二种强转
: 除了上述两种强转外
: 还有什么方式可以转成子类别呢
: 有没有动态的方式能转呢
: 例如有多种子类别实体
: 放在父类别的List变量里
: 要怎么依序的将该List转成子类别呢

Links booklink

Contact Us: admin [ a t ] ucptt.com