111,126
社区成员
发帖
与我相关
我的任务
分享
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方法.
希望对你有帮助!
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();
}
protected IList List
{
get
{
return this;
}
}
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;
}
}
.......