public class Animals:CollectionBase
 {
  public Animals()
  {
  }  public void Add(Animal newAnimal)
  {
   List.Add(newAnimal);
 }......CollectionBase这个抽象继承了IList接口。
我想问的是:
1.为什么属性List可以调用接口IList中的Add方法(),一般对象才能调用方法的不是吗
2.为什么说List是IList接口的一个引用变量?protected IList List { get; } 这样是定义List属性的吧

解决方案 »

  1.   

    为什么说List是IList接口的一个引用变量?
    这么说是错的。
      

  2.   

    人 张三 = new 人(); //这个有问题么?
    张三.说汉语(); //错误,张三是人,不一定说汉语
    人 张三 = new 中国人(); //这个有问题么?
    (张三 as 中国人).说汉语(); //正确
      

  3.   


    public abstract class CollectionBase : IList, ICollection, IEnumerable
    {
        // Fields
        private ArrayList list;
        // Methods
        protected CollectionBase()
        {
            this.list = new ArrayList();
        }
        protected CollectionBase(int capacity)
        {
            this.list = new ArrayList(capacity);
        }    int IList.Add(object value)
        {
            this.OnValidate(value);
            this.OnInsert(this.InnerList.Count, value);
            int index = this.InnerList.Add(value);
            try
            {
                this.OnInsertComplete(index, value);
            }
            catch
            {
                this.InnerList.RemoveAt(index);
                throw;
            }
            return index;
        }
        protected IList List
        {
            get
            {
                return this;
            }
        }
    .......我看到CollectionBase类的源码中CollectionBase显示的实现了IList接口中的 int IList.Add(object value)方法,也定义了protected IList List属性,但是这样就可以用List.Add来调用这个接口中的方法了吗
      

  4.   

    当在CollectionBase类的里面定义了一个IList接口变量List,如果要让List调用CollectionBase类里面重写的Add方法的话,需要把这个变量List指向一个对象的吧,我没找到在CollectionBase的源码中在什么地方指向一个对象了
      

  5.   

    难道是 return this 是指定了对象了吗,this指定的是当前哪个对象呢protected IList List
        {
            get
            {
                return this;
            }
        }
      

  6.   

    和楼主遇到同样问题,给你点参考:
        
        public interface IReview
        {
            void GetReviews();
            void GetSmothing();
        }    public class ShopReview : IReview
        {
            public ShopReview()
            {
            }
            //隐式实现IReview接口
            public void GetReviews()
            {
                System.Console.WriteLine(".....");
            }
            //显示实现IReview接口
            void IReview.GetSmothing()
            {
                System.Console.WriteLine("aaa");
            }
        }调用方式如下:        static void Main(string[] args)
            {
                //隐式实现IReview接口,对象和接口都可调用
                ShopReview sr = new ShopReview();
                sr.GetReviews();
                IReview ir = new ShopReview();
                ir.GetReviews();            //显示实现IReview接口,只能通过接口调用
                ShopReview sr1 = new ShopReview();
                sr1.GetSmothing();//错误,显示实现接口只能通过接口调用
                IReview ir1 = new ShopReview();
                ir1.GetSmothing();
                Console.ReadKey();
            }
      

  7.   

    回到楼主问题:
    1.IList接口的Add方法显示实现方式:    int IList.Add(object value)
        {
            this.OnValidate(value);
            this.OnInsert(this.InnerList.Count, value);
            int index = this.InnerList.Add(value);
            try
            {
                this.OnInsertComplete(index, value);
            }
            catch
            {
                this.InnerList.RemoveAt(index);
                throw;
            }
            return index;
        }只能通过接口去调用改方法.
    2.属性:List    protected IList List
        {
            get
            {
                return this;
            }
        }返回的是由对象转换成IList接口的List属性,同
    IReview ir1 = new ShopReview();
    因此只能通过接口变量List调用Add方法.
    希望对你有帮助!
      

  8.   


    this就是这个类本身的实例。
    如果你的类实现了Ilist接口。那么它的所有方法都可以调用