您的位置:程序门 -> .net技术 -> c#



c#接口实现问题


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


c#接口实现问题[已结贴,结贴人:seamonkey]
发表于:2007-01-31 15:20:45 楼主
在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。我记得好像类必须实现接口的所有方法吧?
发表于:2007-01-31 15:36:511楼 得分:1
对,接口的所有方法必须实现,否则编译报错。声明接口在语法上与声明抽象类完全相同,所以,继承于它的类必须实现它的全部抽象方法,这点在vc里也是这样的。。。。
发表于:2007-01-31 15:44:362楼 得分:1
必须全部实现,否则会出错。
发表于:2007-01-31 15:54:433楼 得分:1
不可以~
发表于:2007-01-31 16:42:524楼 得分:0
可上面的类并不是我写的啊,是微软的system.web.extensions   和ajaxcontroltoolkit库里的源码啊?不知大家装没装微软件ajax1.0,有的话大家可以去找一找ajaxcontroltoolkit,它在:
---ajaxcontroltoolkit解决方案
                      \
                      ajaxcontroltoolkit项目
                      \
                      extenderbase文件夹
                      \
                      profilepropertybindingcollection.cs
发表于:2007-01-31 18:43:145楼 得分:1
可以不全实现这些接口的方法吗?

不能
如果某些接口的成员名称一样,可以只写一次该成员
发表于:2007-01-31 18:53:356楼 得分:0
public   abstract   class     看清楚,是抽象类.     可以不全部实现.
发表于:2007-01-31 19:04:067楼 得分:16
在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
            }


抽象类可以不     "实现 "   接口里的方法的内容
发表于:2007-01-31 20:49:168楼 得分:0
我用ildasm看见它的实现了,原来在ide下面是隐藏啊,呵呵,给你加分分。谢谢


快速检索

最新资讯
热门点击