类A、B、C继承自类ABC
现在我要根据获取到的数据初始化这三个类,并调用三个类的拥有共同名称的函数,我要如何操作呢,请给出具体的代码。比如我将这三个类的对象都放到一个ArrayList数组里,然后从这个ArrayList里取出来的时候,有没有办法直接用对象.属性或对象.函数这种方式操作
现在我要根据获取到的数据初始化这三个类,并调用三个类的拥有共同名称的函数,我要如何操作呢,请给出具体的代码。比如我将这三个类的对象都放到一个ArrayList数组里,然后从这个ArrayList里取出来的时候,有没有办法直接用对象.属性或对象.函数这种方式操作
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Test
{
public abstract class ABC
{
public ABC() { } public abstract void SayHi();
} public class A : ABC
{
public A() { }
public override void SayHi()
{
Console.WriteLine("A");
}
} public class B : ABC
{
public B() { }
public override void SayHi()
{
Console.WriteLine("B");
}
} public class C : ABC
{
public C() { }
public override void SayHi()
{
Console.WriteLine("C");
}
} public class GO
{
static void Main(string[] args)
{
A a = new A();
B b = new B();
C c = new C();
ArrayList abcS = new ArrayList();
abcS.Add(a);
abcS.Add(b);
abcS.Add(c);
foreach (ABC abc in abcS)
{
abc.SayHi();
}
}
}
}
遍历lst,转换为对象,实现操作
有没有这种可能?
我要防止我犯错,所以最好先把犯错的可能收集一下.以便能更深入的理解和应用.
A a=new B();
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Text
{
public interface IOverride
{
void SayHi();
} public abstract class ABC
{
public ABC() { } } public class A : ABC, IOverride
{
public A() { }
public void SayHi()
{
Console.WriteLine("A");
}
} public class B : ABC, IOverride
{
public B() { }
public void SayHi()
{
Console.WriteLine("B");
}
} public class C : ABC, IOverride
{
public C() { }
public void SayHi()
{
Console.WriteLine("C");
}
} class Test
{
public static void Main()
{
A a = new A();
B b = new B();
C c = new C();
ArrayList abcS = new ArrayList();
abcS.Add(a);
abcS.Add(b);
abcS.Add(c);
foreach (IOverride abc in abcS)
{
abc.SayHi();
}
}
}
}