抽象和接口 [英] abstract and interface

查看:86
本文介绍了抽象和接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我们创建抽象类,抽象方法变量等基本上是为什么我们创建抽象类等.

why we create abstract class ,abstractmethod variable etc basically why we create abstract class and etc.

and why we create interfaces and what is interface.

推荐答案

很多解释

请参阅以下讨论:抽象类和接口之间的区别(如果它们相同)方法和变量的数量 [ ^ ].

无论如何,请正确学习这些概念和最佳使用实践.这些是OOP的一些最重要方面.使其成为您定期教育的重点,而不仅仅是依靠特定的问题和答案.

特别是,请从此处开始阅读:
http://msdn.microsoft.com/en-us/library/9cc659c4%28v = vs.71%29.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/scsyfw1d%28v = vs.71%29.aspx [ ^ ].

—SA
Please see this discussion: When we use abstract and when we use interface...?[^].

And also this: Difference between abstract class and interface if they have same no of methods and var[^].

Anyway, learn these concepts and best use practices properly. These are some of the most important aspects of OOP. Make it a point of your regular education, not relying just on particular questions and answers.

In particular, read starting from here:
http://msdn.microsoft.com/en-us/library/9cc659c4%28v=vs.71%29.aspx[^],
http://msdn.microsoft.com/en-us/library/scsyfw1d%28v=vs.71%29.aspx[^].

—SA


简介:

互联网上没有关于接口与抽象类的讨论.另外,无论是必须使用接口,抽象类还是普通类作为基类.

我试图指出一些我们可以决定接口,抽象类和类的考虑因素.



抽象类与接口

我假设您具有abstract和interface关键字的所有基本知识.我只是在介绍基础知识.

我们不能同时创建Abstract类和接口的实例.

根据定义,这是Abstract类和Interface中的一些区别.

抽象类可以包含抽象方法,抽象属性以及其他成员(就像普通类一样).

接口只能包含抽象方法,属性,但我们不需要放入abstract和public关键字.默认情况下,在Interface中定义的所有方法和属性都是public和abstract.


Introduction:

There are lost of discussion on the internet about the Interface vs Abstract class. Also, as base class whether we have to use interface, abstract class or normal class.

I am trying to point out few considerations on which we can take decision about Interface vs Abstract class vs Class.



Abstract Class vs Interface

I am assuming you are having all the basic knowledge of abstract and interface keyword. I am just briefing the basics.

We can not make instance of Abstract Class as well as Interface.

Here are few differences in Abstract class and Interface as per the definition.

Abstract class can contain abstract methods, abstract property as well as other members (just like normal class).

Interface can only contain abstract methods, properties but we don’t need to put abstract and public keyword. All the methods and properties defined in Interface are by default public and abstract.


            //Abstarct Class

public abstract class Vehicles

      {

        private int noOfWheel;

        private string color;

        public abstract string Engine

        {

            get;

            set;

        }

      public abstract void Accelerator();

      }

      //Interface

         public interface Vehicles

      {

        string Engine

        {

            get;

            set;

        }

        void Accelerator();

      }



我们可以看到抽象类包含私有成员,也可以在实现中放入一些方法.但是在使用接口的情况下,只允许使用方法和属性.

我们在应用程序中将抽象类和接口用作基类.



这一切都与语言定义有关.现在百万美元的问题:

如何决定何时必须使用接口以及何时使用抽象类.

基本上,abstact类是任何真实单词实体的抽象视图,而接口则更抽象.当我们考虑实体时,有两件事,一是意图,一是内涵.意图是指我了解实体,也可能对实体的状态和行为有所了解,但不了解实体的外观或工作方式,或者可能不完全了解.实现是指实体的实际状态和行为.

有足够的理论作为例子.

我正在尝试建立一个内容管理系统,其中内容是文章,评论,博客等的概括形式.





内容

文章

博客

评论



因此,内容是我们现在的基础类,我们如何决定内容类应该是Abstract类,Interface还是Normal类.

第一类普通与其他类型(抽象和接口).如果内容不是我的应用程序的核心实体,则按照业务逻辑,如果内容仅在我的应用程序中没有,那么文章,博客,评论是业务逻辑的核心部分,那么内容类不应是普通类,因为我永远不会制作该类的实例.因此,如果您永远都不会成为基类的实例,那么Abstract类和Interface是更合适的选择.

接口和抽象类之间的第二个.

内容

发布()

文章

博客

评论

如您所见,内容的行为名为发布".如果根据我的业务逻辑,发布具有适用于所有对象的某些默认行为,则我将内容类作为Abstract类.如果发布"没有默认行为,并且每个驱动器类都有自己的实现,则在我希望使用接口的基本情况下,无需实现发布"行为.

这些是在抽象类,接口和普通类之间做出决定的一般思想.但是有一个陷阱.众所周知,软件中有一个常数是"CHANGE".如果将内容类作为接口,则很难在基类中进行更改,因为如果在内容接口中添加新方法或属性,则必须在每个驱动器类中实现新方法.如果您对内容类使用抽象类,而新方法不是抽象类型,那么这些问题将会解决.因此,我们可以用抽象类代替除多重继承之外的接口.

CAN-DO和IS-A的关系也定义了接口与抽象类之间的关系.如前所述,接口可以用于多重继承,例如,我们有一个名为"ICopy"的接口,该接口具有行为复制,每个驱动器类都必须实现自己的复制实现.如果"Article"类是从抽象类Content以及ICopy继承而来的,则文章"CAN-DO"也将复制.

IS-A用于泛化"和专业化",是指内容是文章,博客,评论的概括形式,而文章,博客,评论是内容的专门形式.

因此,抽象类定义了核心身份.如果我们考虑速度,那么抽象就是快速接口,因为接口需要额外的间接访问.



We can see abstract class contains private members also we can put some methods with implementation also. But in case of interface only methods and properties allowed.

We use abstract class and Interface for the base class in our application.



This is all about the language defination. Now million doller question:

How can we take decision about when we have to use Interface and when Abstract Class.

Basicly abstact class is a abstract view of any realword entity and interface is more abstract one. When we thinking about the entity there are two things one is intention and one is implemntation. Intention means I know about the entity and also may have idea about its state as well as behaviour but don’t know about how its looks or works or may know partially. Implementation means actual state and behaviour of entity.

Enough theory lets take an example.

I am trying to make a Content Management System where content is a genralize form of article, reviews, blogs etc.





CONTENT

ARTICLE

BLOGS

REVIEW



So content is our base class now how we make a decision whether content class should be Abstract class, Interface or normal class.

First normal class vs other type (abstract and interface). If content is not a core entity of my application means as per the business logic if content is nothing in my application only Article, Blogs, Review are the core part of business logic then content class should not be a normal class because I’ll never make instance of that class. So if you will never make instance of base class then Abstract class and Interface are the more appropriate choice.

Second between Interface and Abstract Class.

CONTENT

Publish ()

ARTICLE

BLOGS

REVIEW

As you can see content having behavior named "Publish". If according to my business logic Publish having some default behavior which apply to all I’ll prefer content class as an Abstract class. If there is no default behavior for the "Publish" and every drive class makes their own implementation then there is no need to implement "Publish" behavior in the base case I’ll prefer Interface.

These are the in general idea of taking decision between abstract class, interface and normal class. But there is one catch. As we all know there is one constant in software that is "CHANGE". If I made content class as Interface then it is difficult to make changes in base class because if I add new method or property in content interface then I have to implement new method in every drive class. These problems will over come if you are using abstract class for content class and new method is not an abstract type. So we can replace interface with abstract class except multiple inheritance.

CAN-DO and IS-A relationship is also define the deference between Interface and abstract class. As we already discuss Interface can be use for multiple inheritance for example we have another interface named "ICopy" which having behavior copy and every drive class have to implements its own implementation of Copy. If "Article" class drive from abstract class Content as well as ICopy then article "CAN-DO" copy also.

IS-A is for "generalization" and "specialization" means content is a generalize form of Article, Blogs, Review and Article, Blogs, Review are a specialize form of Content.

So, abstract class defines core identity. If we are thinking in term of speed then abstract is fast then interface because interface requires extra in-direction.


这篇关于抽象和接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆