想问一下CollectionBase.List 属性的问题

打小不爱吃饭 2012-04-18 04:08:38
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属性的吧
...全文
196 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
dulei_nn 2013-02-19
  • 打赏
  • 举报
回复
新手刚开始学习C#,描述有什么问题请大神指教!
  • 打赏
  • 举报
回复
引用 8 楼 mushanxiaozhupipi 的回复:
难道是 return this 是指定了对象了吗,this指定的是当前哪个对象呢 C# code?1234567protected IList List { get { return this; } }
this就是这个类本身的实例。 如果你的类实现了Ilist接口。那么它的所有方法都可以调用。。。
dulei_nn 2013-02-19
  • 打赏
  • 举报
回复
回到楼主问题: 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方法. 希望对你有帮助!
dulei_nn 2013-02-19
  • 打赏
  • 举报
回复
和楼主遇到同样问题,给你点参考:
    
    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();
        }
打小不爱吃饭 2012-04-19
  • 打赏
  • 举报
回复
难道是 return this 是指定了对象了吗,this指定的是当前哪个对象呢

protected IList List
{
get
{
return this;
}
}
打小不爱吃饭 2012-04-19
  • 打赏
  • 举报
回复
哪位大侠能帮回答一下这个问题呢
打小不爱吃饭 2012-04-19
  • 打赏
  • 举报
回复
当在CollectionBase类的里面定义了一个IList接口变量List,如果要让List调用CollectionBase类里面重写的Add方法的话,需要把这个变量List指向一个对象的吧,我没找到在CollectionBase的源码中在什么地方指向一个对象了
test2050 2012-04-19
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]
C# code

public abstract class CollectionBase : IList, ICollection, IEnumerable
{
// Fields
private ArrayList list;
// Methods
protected CollectionBase()
{
this.li……
[/Quote]
此代码中可以下断点的话,你就一步步跟踪下看代码是怎么执行的。对你的理解有帮助。
打小不爱吃饭 2012-04-19
  • 打赏
  • 举报
回复

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来调用这个接口中的方法了吗
打小不爱吃饭 2012-04-18
  • 打赏
  • 举报
回复
我也看了有关接口的定义,但是当遇到实际的问题的时候,还是有些搞不清楚
threenewbee 2012-04-18
  • 打赏
  • 举报
回复
人 张三 = new 人(); //这个有问题么?
张三.说汉语(); //错误,张三是人,不一定说汉语
人 张三 = new 中国人(); //这个有问题么?
(张三 as 中国人).说汉语(); //正确
threenewbee 2012-04-18
  • 打赏
  • 举报
回复
为什么说List是IList接口的一个引用变量?
这么说是错的。
bdmh 2012-04-18
  • 打赏
  • 举报
回复
自己去搞清楚接口是怎回事

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧