多态性和接口 - 澄清?

(迂腐问题)

根据 wikipedia 有 3 种类型多态性:

According to wikipedia there are 3 types of polymorphism :

  • 即席多态性

指的是可以应用于参数的多态函数不同的类型,但根据不同的类型表现不同应用它们的论点

refer to polymorphic functions which can be applied to arguments of different types, but which behave differently depending on the type of the argument to which they are applied

换句话说:重载:

function Add( x, y : Integer ) : Integer;
...
function Add( s, t : String ) : String;

  • 参数多态性
  • 允许通用地编写函数或数据类型,以便它可以相同地处理值而不依赖于它们的类型

    allows a function or a data type to be written generically, so that it can handle values identically without depending on their type

    换句话说:泛型

    例子:

    class List<T> {
        class Node<T> { ...
    

    • 亚型多态性
    • 允许编写一个函数来获取某个类型 T 的对象,但如果传递属于类型 S 的对象也能正常工作这是T的一个子类型

      allows a function to be written to take an object of a certain type T, but also work correctly if passed an object that belongs to a type S that is a subtype of T

      (最常见的用法)

      例子:

      abstract class Animal {
          abstract String talk();
      }
      
      class Cat extends Animal {
          String talk() {
              return "Meow!";
          }
      }
      ...
      

      另一个例子:

      class Animal
      {
          public virtual void eat()
          {
              Console.Write("Animal eating");
          }
      }
      class Dog : Animal
      {
          public override void eat()
          {
              Console.Write("Dog eating");
          }
      }
      

      太棒了.

      现在我想告诉你接口的定义:

      Now I would like to show you the definition of interface :

      接口 - 接口定义了一个契约,可以通过类和结构.接口可以包含方法、属性、事件和索引器.接口不提供以下实现它定义的成员——它只指定必须是的成员由实现接口的类或结构提供.

      Interfaces - An interface defines a contract that can be implemented by classes and structs. An interface can contain methods, properties, events, and indexers. An interface does not provide implementations of the members it defines—it merely specifies the members that must be supplied by classes or structs that implement the interface.

      太棒了.

      问题:

      看看这个伪代码:

      Dog implements IWalk {...}
      Cat implements IWalk {...}
      Array[IWalk] = [new Dog(),new Cat()]
      foreach item in array :  item.walk();
      

      • 这是多态行为(在每个不同的对象上调用 walk())吗?
      • 恕我直言,事实并非如此.为什么 ?因为它不对应于上面提到的任何 wiki 类别.

        IMHO it is not. why ? because it doesn't corresponds to any of wiki categories mentioned above.

        我相信这是纯粹的编码原则,我通过不同的眼镜看一个对象——而不是像上面提到的 3 个范例那样创建/改变功能

        我说的对吗?这是多态行为吗?

        Am I right ? is this polymorphic behaviour or not ?

        推荐答案

        我认为你是在正确的轨道上.接口本身不是多态的.它们形式化了多态性.它们允许您以声明的方式定义多态行为,而不是实现.我喜欢把 Interfaces 想象成俱乐部里的保镖.他们只是确保每个人都遵循多态规则.

        I think you are on the right track. Interfaces are not polymorphic themselves. They formalize polymorphism. They allow you to define polymorphic behavior in a declarative way, instead of implementation. I like to think of Interfaces as the bouncers in the club. They just make sure everyone is following the polymorphic rules.

        在您的示例中,实际的多态行为与接口本身无关,而是与共享的方法相关.walk 方法适合子类型多态性示例.接口本身只是根据合同要求子对象行走.

        In your example the actual polymorphic behavior isn't related to the interface itself but the method that is shared. The walk method fits the subtype polymorphism example. The interface itself just contractually obligates the child objects to walk.

        更新:

        基于评论:为了清楚起见 - 通过对象实现的不同接口查看对象 - 也是多态行为?

        based on comment: just to make it clear - looking at an object via different interfaces it implements - is also polymorphic behaviour ?

        界面本身只是一个合同(您在问题中指出).多态性来自合约上的方法.多态类型是一种其操作也可以应用于某些其他类型或类型的值的类型"(来自维基百科).接口(合同)是保存服务或使用方法(条款)的协议.所以每个合约都包含多态行为的条款.

        The interface itself is just a contract (which you nailed in your question). The polymorphism comes from the methods on the contract. "A polymorphic type is a type whose operations can also be applied to values of some other type, or types" (from wikipedia). The interface (contract) is the agreement that holds the methods (terms) of service or use. So each contract hold the terms that are the polymorphic behavior.

        Dog implements IWalk, ITalk, IAnimal
        {
            //concrete implementation
        }
        
        IWalk 
        {
            void walk();
        }
        
        ITalk
        {
            String talk();
        }
        
        IAnimal
        {
        
        }
        

        我对 java 生疏了,所以如果它在语法上不正确,请考虑这个伪代码.

        I am rusty on java so consider this pseudocode if its not syntactically correct.

        在这种情况下,IAnimal 只是一个不包含多态行为的合约.IWalk 和 ITalk 促进了多态性.

        IAnimal in this case is just a contract that does not contain polymorphic behavior. IWalk and ITalk promote polymorphism.

相关文章