×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

Actually abstract class is quite different from interface.

本文发表在 rolia.net 枫下论坛* Interface only contains unimplemented methods /properties. You cannot specify fields in an interface. On the other hand, abstract class has full features of class, such as normal/virtual/static methods/properties, and fields, in addition abstract methods/properties for child class to implement.

* If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes.

* If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class.

* If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members.

* If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface.更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / 学科技术讨论 / C#中, 声明一个抽象类, 继承于一个接口, 那么在抽象类中需要实现接口函数吗?
    • 兄台,能不能用英文写专业术语,让俺也能看懂点
    • 你可以自己试,看编译器能通过否,有结论后,在更新一下帖子
    • All methods in interface has to be implemented in child class no mater it’s abstract or normal.
      • Yes
    • Actually, MS already suggest not using abstract class, use interface instead. Because some Rumtime issue. If I remmeber correctly
      • Never heard about the issue. You might misunderstand.
        There are technical issues with multiple inheritance, so many OO languages restrict inheritance to a single direct base class (either normal or abstract). However multiple- interface inheritance is not limited. This doesn’t mean you cannot use abstract class.
      • 正好相反
        • en, 在微软出的一本书中看到过作者对抽象类和接口的说明,那小子所在的项目第一版全都用的接口,然后后悔不已。第二版全都改成了抽象类 ^_^
      • As a base class in project. We can make our decision as follows:
        if you need "multiple inheritance", you must use Interface. If the base class will be changed often, I prefer abstract class. If you had some "default behaviour" that will implement in every child class, then just use a normal class. For other situation do as you like.
      • Actually abstract class is quite different from interface.
        本文发表在 rolia.net 枫下论坛* Interface only contains unimplemented methods /properties. You cannot specify fields in an interface. On the other hand, abstract class has full features of class, such as normal/virtual/static methods/properties, and fields, in addition abstract methods/properties for child class to implement.

        * If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes.

        * If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class.

        * If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members.

        * If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface.更多精彩文章及讨论,请光临枫下论坛 rolia.net
    • 借你帖问一个编程构架问题: WEB APPLICATION 就没必要多层次OO了. ? 一个PAGEBASE大家INHERIT就够了?
      PAGE POSTBACK 后OO中的CLASS FIELD (PUBLIC)就不能用了. 我只能用VIEWSTATE来代替.
      也许我还不懂更高深的... (我只是入门菜鸟)
      • cannot understand
        • 表达有问题. 我的意思是WEB APPLICATION 没必要复杂OO.
      • OO好处是代码重用。我碰到一位同胞把同样的代码copy到几十个地方,跟我谈心得:copy完了再加点新东西,又可以和领导说新写了多少多少代码。NND他现在跳槽走了,让我接手这垃圾,同样的错误一发现就要改几十个地方。
        • OO本意是为了系统容易维护,class代码相对独立,这个人代码肯定不是OO,设计好的class,不会改这么多地方。 OO其实也没什么用,MS牛B, MS .Net framework 那么多class, 前后版本都不兼容,说明OO对大系统没什么意义,unix/linux还是用C
          • 为啥要前后版本兼容,把.NET Framework 1.1/2.0/3.0/3.5全装上就是了,一个也就20-30兆大小,免费。
            • 你装过没?20-30M ? 太小看MS ,运行一个.net exe,要装一堆东西,还分版本, 太烂了
              • 只能是说你在不懂装懂。
                • Microsoft .NET Framework Version 1.1 Redistributable ==>
                • Microsoft .NET Framework Version 2.0 Redistributable ==>
                • Microsoft .NET Framework 3.0 Redistributable ==>
            • that's right. my working environment was installed all versions of .net framework
        • 也不一定都是垃圾吧. 把去ROMA的路搞复杂点也是一种求生之道.
          • 所言极是。
        • 你也跟领导说你一天就改了几十个地方不 就行了 , FT, 你还不领情
          • 所言极是。