接口继承的声明问题
2007-01-17 21:23
435 查看
Written by Allen Lee
某天,小新问我这样一个问题:
类System.Collections.CollectionBase是从IList、ICollection继承而来,IList是从ICollection和IEnumerable继承而来,那CollectionBase为什么还要从ICollection继承呢?
我们先来看看这些类和接口在MSDN文档中的声明:
根据接口继承的规则,我们知道CollectionBase只需要声明实现IList,就必须同时实现ICollection,也就必须实现IEnumerable,那么,我们为什么还要明确地把所有的这些接口都写下来呢?
换句话说,下面两种声明没有实质的区别:
那为何MSDN要使用上面那种呢?我和小新讨论后,一致认为这样做仅仅为了提高代码的可读性。为了验证我们的想法,我分别发邮件给Eric Gunnerson(Eric是C# Compiler Team的成员)和Kit George(Kit是BCL Team的成员)询问这个问题,他们的回信如下:
今天,我查看微软的Rotor源代码,发现ArrayList的声明的确是Code #1的做法,不过Mono(ver. 1.1.2 Development Version)就采用了Code #2的做法。
所以,以后如果你再碰到到这样的情况,你可以轻松的笑一声:“这样做是为了提高代码的可读性的!”
// Code #2
public interface IEnumerable
public interface ICollection : IEnumerable
public interface IList : ICollection
public class ArrayList : IList, ICloneable
// Code #1
public interface IEnumerable
public interface ICollection : IEnumerable
public interface IList : ICollection, IEnumerable
public class ArrayList : IList, ICollection, IEnumerable, ICloneable
public interface IEnumerable
public interface ICollection : IEnumerable
public interface IList : ICollection, IEnumerable
public abstract class CollectionBase : IList, ICollection, IEnumerable
某天,小新问我这样一个问题:
类System.Collections.CollectionBase是从IList、ICollection继承而来,IList是从ICollection和IEnumerable继承而来,那CollectionBase为什么还要从ICollection继承呢?
我们先来看看这些类和接口在MSDN文档中的声明:
根据接口继承的规则,我们知道CollectionBase只需要声明实现IList,就必须同时实现ICollection,也就必须实现IEnumerable,那么,我们为什么还要明确地把所有的这些接口都写下来呢?
换句话说,下面两种声明没有实质的区别:
那为何MSDN要使用上面那种呢?我和小新讨论后,一致认为这样做仅仅为了提高代码的可读性。为了验证我们的想法,我分别发邮件给Eric Gunnerson(Eric是C# Compiler Team的成员)和Kit George(Kit是BCL Team的成员)询问这个问题,他们的回信如下:
Allen, I think that readability would be the primary reason people would do this. Eric |
Allen, what you’re seeing is simply a doc thing. ArrayList actually only implements IList directly, but we decided in V1.0 of the docs, to highlight the interface hierarchy so you didn’t have to wonder ‘what does IList implement’, you get to see it there on the type. There is no benefit to ACTUALLY doing this. Try this code, and you’ll see it compiles: Regards, Kit using System; using System.IO; public class Test : IBob2 { void IBob.M() {} } interface IBob { void M(); } interface IBob2 : IBob {} |
所以,以后如果你再碰到到这样的情况,你可以轻松的笑一声:“这样做是为了提高代码的可读性的!”
// Code #2
public interface IEnumerable
public interface ICollection : IEnumerable
public interface IList : ICollection
public class ArrayList : IList, ICloneable
// Code #1
public interface IEnumerable
public interface ICollection : IEnumerable
public interface IList : ICollection, IEnumerable
public class ArrayList : IList, ICollection, IEnumerable, ICloneable
public interface IEnumerable
public interface ICollection : IEnumerable
public interface IList : ICollection, IEnumerable
public abstract class CollectionBase : IList, ICollection, IEnumerable
相关文章推荐
- 接口继承的声明问题 [C#, BCL]
- 接口继承的声明问题
- 基类与接口混合继承的声明问题 [C#, Design]
- 基类与接口混合继承的声明问题 [C#, Design] -Allen Lee's Magic
- 接口继承的声明问题 [C#, BCL]
- 基类与接口混合继承的声明问题 [C#, Design]
- 接口继承的声明问题
- ava继承:重新使用接口问题
- java接口中多继承的问题
- 关于接口的实现和继承的问题
- java中的继承问题,类B继承A,那么构造方法需要这样写吗?这不是重复吗?还有接口与抽象类怎么实现?
- 实现接口的类是否继承接口实现的问题
- 关于接口的实现和继承的问题
- java接口的继承问题
- 接口声明-抽象类空实现-继承重写来实现适配器的模式
- *继承IObjectSafety接口,实现vb activeX控件安全性(IE不提示安全问题)
- 关于java中继承多接口实现其中同名方法的问题
- C#类继承和接口继承时一些模棱两可的问题[转]
- Java小程序---接口中抽象方法的实现(解决了JAVA语言不能多继承的问题)
- 继承类或实现接口时对原有方法覆盖时异常抛出声明的规则