类A、B、C继承自类ABC
现在我要根据获取到的数据初始化这三个类,并调用三个类的拥有共同名称的函数,我要如何操作呢,请给出具体的代码。比如我将这三个类的对象都放到一个ArrayList数组里,然后从这个ArrayList里取出来的时候,有没有办法直接用对象.属性或对象.函数这种方式操作

解决方案 »

  1.   


    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();
                }
            }
        }
    }
      

  2.   

    List<T> lst=new List<T>();
    遍历lst,转换为对象,实现操作
      

  3.   

    List <ABC> lst=new List <ABC>(); 
      

  4.   

    嘿嘿,我C#不是很专业,我怕一不小心,系统真的调用了基类的SayHi()了.
    有没有这种可能?
    我要防止我犯错,所以最好先把犯错的可能收集一下.以便能更深入的理解和应用.
      

  5.   

    在类ABC写虚函数,子类中实现就行了
      

  6.   

    运行时的对象范围限定在代码编译时,但不等于编译时的对象。
    A a=new B();
      

  7.   

    可以的,并且接口可多实现,类似于多继承,接口中定义好的成员,不用去实现,只要在实现它的类中实现就行了,不需要overrideusing System;
    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();
                }
            }
        }
    }