是接口兼容与多态性 [英] Are Interfaces Compatible With Polymorphism

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

问题描述

我有与多态类型(甚至是多态的接口)的交互接口的概念麻烦。我正在开发的C#,并希望答案下榻的关闭的这个定义,但我认为仍然给出足够的空间给大家提出一个答案。

I am having trouble with the concept of interfaces interacting with polymorphic types (or even polymorphic interfaces). I'm developing in C# and would appreciate answers staying close to this definition, although i think that still gives plenty of room for everyone to put forth an answer.

只是作为一个例子,假设你想使一个程序来画的东西。您可以定义为绘制演员的接口,你定义它是画的主题界面,而且你有一些科目,可以在一个更具体的方式来画。

Just as an example, let's say you want to make a program to paint things. You define an interface for the actor that Paints, and you define an interface for the subject which is painted, Furthermore you have some subjects which can be painted in a more specific way.

interface IPainter {
  void paint(IPaintable paintable);
}
interface IPaintable {
  void ApplyBaseLayer(Color c);
}
interface IDecalPaintable : IPaintable {
  void ApplyDecal(HatchBrush b);
}



我能想象做出类似如下的画家:

I can imagine making a painter similar to the following:

class AwesomeDecalPainter : IPainter
  {
    public void paint(IPaintable paintable) {
      IDecalPaintable decalPaintable = (IDecalPaintable)paintable;
      decalPaintable.ApplyBaseLayer(Color.Red);
      decalPaintable.ApplyDecal(new HatchBrush(HatchStyle.Plaid, Color.Green));
    }
  }



当然,这将抛出如果上漆没有实现IDecalPaintable 。它马上介绍了IPainter实施和IPaintable它运行在之间的耦合。不过我也并不认为这是有道理的说,AwesomeDecalPainter不是IPainter只是因为它的使用仅限于IPaintable域的一个子集。

Of course this will throw if paintable does not implement IDecalPaintable. It immediately introduces a coupling between the IPainter implementation and the IPaintable that it operates on. However I also don't think it makes sense to say that AwesomeDecalPainter is not an IPainter just because it's use is limited to a subset of the IPaintable domain.

所以我的问题真的是四个方面:

So my question is really four-fold:


  • 是接口
    多态性在所有兼容?

  • 这是好
    设计,以实现一个IPainter是
    可以IDecalPaintable操作?

  • 怎么样,如果它可以完全放在IDecalPaintable操作

  • 是否有
    体现接口和多态类型
    应该如何相互作用?任何文献或源代码

  • Are interface compatible with polymorphism at all?
  • Is it good design to implement an IPainter that can operate on IDecalPaintable?
  • What about if it can exclusively operate on IDecalPaintable?
  • Is there any literature or source code that exemplifies how interfaces and polymorphic types should interact?

推荐答案

一类的接口是为了作为这个类的用户的工具。接口是该类的公开介绍,它应该做广告,给任何人考虑使用它,有什么方法和常量可从外部访问。如此,因为它的名字所暗示的,它始终在用户和执行它的类之间坐在

The interface of a class is meant as a tool for the "user" of that class. An interface is a public presentation for the class, and it should advertise, to anyone considering to use it, what methods and constants are available and accessible from the outside. So, as it name suggests, it always sits between the user and the class implementing it.

在另一方面,一个抽象类是旨在帮助的工具实现者扩展它的类。这是一个可以并处关于具体的类应该是什么样子的限制和准则的基础设施。从类设计的角度来看,抽象类不是接口更重要的建筑。在这种情况下,实现者坐在抽象类和混凝土之一之间,建立在前者的顶部后者

On the other hand, an abstract class is a tool aimed at helping the "implementor" of the classes that extend it. It is an infrastructure that can impose restrictions and guidelines about what the concrete classes should look like. From a class design perspective, abstract classes are more architecturally important than interfaces. In this case, the implementor sits between the abstract class and the concrete one, building the latter on top of the former.

因此​​,为了简单地回答您的问题,接口是一个合同的代码的尊重。当uused这种方式,它更适用于继承的多态性。

So to answer your question simply, Interface is a "contract" for the code to respect. When uused this way, it applies more to inheritance that polymorphism.

抽象类,它们定义了一个类型。而当具体子类使用抽象类和重新定义的方法,添加新的等......还有你看到的多态性在行动。

Abstract classes, they define a 'type'. And when the concrete sub classes use abstract classes and redefine methods, add new ones etc ... there you see polymorphism in action.

我知道这个职位可能更迷惑你,它没有任何意义,我,直到我学会了设计模式。用你的腰带几个简单的模式,你会更好地理解并进,打造简洁的设计,应用程序的每个对象以及如何继承,多态和封装玩手的作用。

I know this post may confuse you more, it didn't make sense to me until I learned design patterns. With a few simple patterns under your belt you will better understand the role of each object and how inheritance, polymorphism and encapsulation play hand in hand to build cleanly designed applications.

好-Luck

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

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