读书人

C#接口实现有关问题

发布时间: 2011-12-20 22:26:41 作者: rapoo

C#接口实现问题
在C#一个类从几个接口派生,可以不全实现这些接口的方法吗?代码如下:

相关接口
==================================================================
// 摘要:
// 表示可按照索引单独访问的对象的非泛型集合。
[ComVisible(true)]
public interface IList : ICollection, IEnumerable
{
// 摘要:
// 获取一个值,该值指示 System.Collections.IList 是否具有固定大小。
//
// 返回结果:
// 如果 System.Collections.IList 具有固定大小,则为 true;否则为 false。
bool IsFixedSize { get; }
//
// 摘要:
// 获取一个值,该值指示 System.Collections.IList 是否为只读。
//
// 返回结果:
// 如果 System.Collections.IList 为只读,则为 true;否则为 false。
bool IsReadOnly { get; }

// 摘要:
// 获取或设置指定索引处的元素。
//
// 参数:
// index:
// 要获得或设置的元素从零开始的索引。
//
// 返回结果:
// 指定索引处的元素。
object this[int index] { get; set; }

// 摘要:
// 将某项添加到 System.Collections.IList 中。
//
// 参数:
// value:
// 要添加到 System.Collections.IList 的 System.Object。
//
// 返回结果:
// 新元素的插入位置。
int Add(object value);


//
// 摘要:
// 从 System.Collections.IList 中移除所有项。
void Clear();
//
// 摘要:
// 确定 System.Collections.IList 是否包含特定值。
//
// 参数:
// value:
// 要在 System.Collections.IList 中查找的 System.Object。
//
// 返回结果:
// 如果在 System.Collections.IList 中找到 System.Object,则为 true;否则为 false。
bool Contains(object value);
//
// 摘要:
// 确定 System.Collections.IList 中特定项的索引。
//
// 参数:
// value:
// 要在 System.Collections.IList 中查找的 System.Object。
//
// 返回结果:
// 如果在列表中找到,则为 value 的索引;否则为 -1。
int IndexOf(object value);
//
// 摘要:
// 将一个项插入指定索引处的 System.Collections.IList。
//
// 参数:
// value:
// 要插入 System.Collections.IList 中的 System.Object。
//
// index:
// 从零开始的索引,应在该位置插入 value。
void Insert(int index, object value);
//


// 摘要:
// 从 System.Collections.IList 中移除特定对象的第一个匹配项。
//
// 参数:
// value:
// 要从 System.Collections.IList 移除的 System.Object。
void Remove(object value);
//
// 摘要:
// 移除指定索引处的 System.Collections.IList 项。
//
// 参数:
// index:
// 从零开始的索引(属于要移除的项)。
void RemoveAt(int index);
}

// 摘要:
// 定义所有非泛型集合的大小、枚举数和同步方法。
[ComVisible(true)]
public interface ICollection : IEnumerable
{
// 摘要:
// 获取 System.Collections.ICollection 中包含的元素数。
//
// 返回结果:
// System.Collections.ICollection 中包含的元素数。
int Count { get; }
//
// 摘要:
// 获取一个值,该值指示是否同步对 System.Collections.ICollection 的访问(线程安全)。
//
// 返回结果:
// 如果对 System.Collections.ICollection 的访问是同步的(线程安全),则为 true;否则为 false。
bool IsSynchronized { get; }
//
// 摘要:
// 获取可用于同步 System.Collections.ICollection 访问的对象。
//
// 返回结果:
// 可用于同步对 System.Collections.ICollection 的访问的对象。


object SyncRoot { get; }

// 摘要:
// 从特定的 System.Array 索引处开始,将 System.Collections.ICollection 的元素复制到一个 System.Array
// 中。
//
// 参数:
// array:
// 作为从 System.Collections.ICollection 复制的元素的目标位置的一维 System.Array。System.Array
// 必须具有从零开始的索引。
//
// index:
// array 中从零开始的索引,从此处开始复制。
void CopyTo(Array array, int index);
}

// 摘要:
// 公开枚举数,该枚举数支持在非泛型集合上进行简单迭代。
[ComVisible(true)]
[Guid( "496B0ABE-CDEE-11d3-88E8-00902754C43A ")]
public interface IEnumerable
{
// 摘要:
// 返回一个循环访问集合的枚举数。
//
// 返回结果:
// 可用于循环访问集合的 System.Collections.IEnumerator 对象。
[DispId(-4)]
IEnumerator GetEnumerator();
}
===================================================================

[Serializable]
[ComVisible(true)]
public abstract class CollectionBase : IList, ICollection, Enumerable
{
protected CollectionBase();
protected CollectionBase(int capacity);
public int Capacity { get; set; }
public int Count { get; }
protected ArrayList InnerList { get; }


protected IList List { get; }
protected virtual void OnClear();
protected virtual void OnClearComplete();
protected virtual void OnInsert(int index, object value);
protected virtual void OnRemove(int index, object value);
protected virtual void OnSet(int index, object oldValue, object newValue);
protected virtual void OnSetComplete(int index, object oldValue, object newValue);
protected virtual void OnValidate(object value);
public void RemoveAt(int index);
//
public class ProfilePropertyBindingCollection : CollectionBase
{
internal event EventHandler CollectionChanged;

internal ProfilePropertyBindingCollection()
{
}

public ProfilePropertyBinding this[int index]
{
get
{
return (ProfilePropertyBinding)InnerList[index];
}
set
{
InnerList[index] = value;
}
}

public void Add(ProfilePropertyBinding binding) {
InnerList.Add(binding);
}

public void Insert(int index, ProfilePropertyBinding binding)


{
InnerList.Insert(index, binding);
}

protected virtual void OnCollectionChanged(EventArgs e)
{
if (CollectionChanged != null)
{
CollectionChanged(this, e);
}
}

public void Remove(ProfilePropertyBinding binding) {
InnerList.Remove(binding);
}

protected override void OnInsertComplete(int index, object value)
{
base.OnInsertComplete(index, value);
OnCollectionChanged(EventArgs.Empty);
}

protected override void OnSetComplete(int index, object oldValue, object newValue)
{
base.OnSetComplete(index, oldValue, newValue);
OnCollectionChanged(EventArgs.Empty);
}

protected override void OnRemoveComplete(int index, object value)
{
base.OnRemoveComplete(index, value);
OnCollectionChanged(EventArgs.Empty);
}

protected override void OnClearComplete()
{
base.OnClearComplete();
OnCollectionChanged(EventArgs.Empty);
}
}
}

//在以上的类实现过程中,有好几个接口的方法都没有实现,如IList下的 bool Contains。我记得好像类必须实现接口的所有方法吧?



[解决办法]
对,接口的所有方法必须实现,否则编译报错。声明接口在语法上与声明抽象类完全相同,所以,继承于它的类必须实现它的全部抽象方法,这点在VC里也是这样的。。。。
[解决办法]
必须全部实现,否则会出错。
[解决办法]
不可以~
[解决办法]
可以不全实现这些接口的方法吗?

不能
如果某些接口的成员名称一样,可以只写一次该成员
[解决办法]
在CollectionBase抽象类中是有的:
int IList.Add(object value);
在RemoveAt方法的后面.因为是抽象类,只是写一下没有 "实现 " 出来所以一些反编译工具比如Reflector 会将其从列表中隐藏.

如果你用ILDASM命令看IL是可以看见这些方法的:
.method private hidebysig newslot virtual final instance int32 System.Collections.IList.Add(object value) cil managed
{
.override System.Collections.IList::Add
}


抽象类可以不 "实现 " 接口里的方法的内容

读书人网 >C#

热点推荐