什么时候必须使用Abstract类以及何时必须使用Interface? [英] When we have to use Abstract class and when we have to use Interface ?

查看:65
本文介绍了什么时候必须使用Abstract类以及何时必须使用Interface?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

何时必须使用Abstract类以及何时必须使用Interface?

When we have to use Abstract class and when we have to use Interface ?

推荐答案

在适当的时候.

如果您只是在学习,请找一本书并尝试使用它们.
When appropriate.

If you are just learning, pick up a book and try using each of them.


委托和接口都允许类设计器将类型声明和实现分开.

接口描述了一组可以属于任何类或结构的相关行为.接口可以由方法,属性,事件,索引器或这四种成员类型的任意组合组成.接口不能包含字段.接口成员自动公开.

给定的接口可以由任何Class继承和实现.

委托类似于C或C ++中的函数指针.使用委托可以使程序员将对方法的引用封装在委托对象中.

可以为任何类上的方法创建委托,只要该方法适合该委托的方法签名即可.一个对象可以在不了解实现接口或委托方法的类的情况下使用接口引用或委托.鉴于这些相似之处,类设计人员何时应使用委托,何时应使用接口?
何时使用委托人:
•使用了事件设计模式.
•最好封装一个静态方法.
•调用方无需访问实现该方法的对象上的其他属性,方法或接口.
•需要简单的组成.
•一个类可能需要该方法的多个实现.
何时使用界面:
•可以调用一组相关方法.
•类仅需要该方法的一个实现.
•使用该接口的类将需要将该接口转换为其他接口或类类型.
•正在实现的方法链接到类的类型或标识:例如,比较方法.

由msdn help
Both delegates and interfaces allow a class designer to separate type declarations and implementation.

Interfaces describe a group of related behaviors that can belong to any class or struct. Interfaces can be made up of methods, properties, events, indexers, or any combination of those four member types. An interface can not contain fields. Interfaces members are automatically public.

A given interface can be inherited and implemented by any Class.

A delegate is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object.

A delegate can created for a method on any class, as long as the method fits the method signature for the delegate. An interface reference or a delegate can be used by an object with no knowledge of the class that implements the interface or delegate method. Given these similarities, when should a class designer use a delegate and when should they use an interface?
When to use a delegate:
• An eventing design pattern is used.
• It is desirable to encapsulate a static method.
• The caller has no need access other properties, methods, or interfaces on the object implementing the method.
• Easy composition is desired.
• A class may need more than one implementation of the method.
When to use an interface:
• There are a group of related methods that may be called.
• A class only needs one implementation of the method.
• The class using the interface will want to cast that interface to other interface or class types.
• The method being implemented is linked to the type or identity of the class: for example, comparison methods.

by msdn help


太阳可以使用抽象类任何对象"
(您或我,那是抽象的,而不是具体的石头)

和一个通用的界面功能加热"
(可以在具体类的每个对象上以不同方式实现).

太阳一定不知道您的任何实现或修改,
它说每天都一样,在任何物体"处:变得温暖":)
The sun could use an abstract class "any object"
(that is abstract, not concretely a stone, you or me)

and a general interface function "be warm"
(that could be differently implemented at each object of a concrete class).

The sun must not know any implemetation or its modification by you,
it say everyday just the same, at "any object": "be warm" :)


这篇关于什么时候必须使用Abstract类以及何时必须使用Interface?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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