什么是接口鸭打字? [英] What is Interface Duck Typing?

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

问题描述

我听到这个词接口鸭打字,但根本不又是什么理解?所以,我读了这个wiki和他们说:




在计算机编程与面向对象编程语言,鸭打字打字风格在其中一个对象的方法和属性决定了有效的语义,而不是显式接口的一个特定的类或实现其继承。这个概念的名称指的是鸭子测试。




但还是没能明白它。所以,我看到了他们的计划,但他们使用动态关键字调用嘎嘎()&安培; 所有类的羽()功能。



我会要求大家请简单的方式解释一下什么是接口鸭子类型,以及如何在C#2.0实现,因为没有动态关键字。

 使用系统; 

命名空间鸭子类型
{
公共类鸭
{
公共无效嘎嘎()
{
Console.WriteLine( Quaaaaaack)!;
}

公共无效羽毛()
{
Console.WriteLine(鸭子有白色和灰色的羽毛。);
}
}

公共类Person
{
公共无效嘎嘎()
{
Console.WriteLine(以下简称人模仿鸭子)。
}

公共无效羽毛()
{
Console.WriteLine(这个人从地上取一根羽毛,并显示它。);
}
}

内部类节目
{
私有静态无效InTheForest(动态鸭)
{
duck.Quack ();
duck.Feathers();
}

私有静态无效游戏()
{
鸭唐纳德=新鸭();
人约翰=新的Person();
InTheForest(唐纳德);
InTheForest(约翰);
}

私有静态无效的主要()
{
比赛();
}
}
}


解决方案

鸭打字允许对象在给需要$ b $巴某些类型即使它不从该类型继承的方法进行传递。所有它具有
键做的是支持该方法在
使用所期望的类型的方法和属性。我强调一个原因,最后一句。假设
我们有发生在一个鸭实例的方法,和另一种方法,
,取入兔实例。在这种
支持鸭打字动态类型语言,我可以在我的对象传递给第一种方法为
只要我的对象支持通过该方法的方法和使用
鸭的属性。同样地,因为它支持的方法和兔由
中的第二个方法调用的属性我可以通过我的对象到第二方法
一样长。是我的对象鸭子还是兔子?像
上图中,它既不是,它是两者兼而有之。在许多(如果不是大多数)的动态
语言,我的对象不具有支持要传递到期望一个鸭子的方法鸭子的所有方法和
属性。
也是一样的期望一个rabbit.It只需要支持
这实际上是
。通过该方法被称为预期类型的​​方法和属性的方法。




请参阅此获得关于鸭打字



http://haacked.com/archive/2007/08/19 /why-duck-typing-matters-to-c-developers.aspx/


I heard the word Interface Duck Typing, but do not understand at all what is it? So I read a wiki about this and they said:

In computer programming with object-oriented programming languages, duck typing is a style of typing in which an object's methods and properties determine the valid semantics, rather than its inheritance from a particular class or implementation of an explicit interface. The name of the concept refers to the duck test.

But still could not understand what it. So I saw their program but they use dynamic keyword to call quack() & feather() function of all the classes.

I would request you all please explain in easy way what is Interface Duck Typing and how to implement in C# v2.0 because there is no dynamic keyword.

using System;

namespace DuckTyping 
{  
  public class Duck 
  {
    public void Quack() 
    {
      Console.WriteLine("Quaaaaaack!");
    }

    public void Feathers() 
    {
      Console.WriteLine("The duck has white and gray feathers.");
    }
  }

  public class Person 
  {
    public void Quack()
    {
      Console.WriteLine("The person imitates a duck.");
    }

    public void Feathers() 
    {
      Console.WriteLine("The person takes a feather from the ground and shows it.");
    }
  }

  internal class Program 
  {
    private static void InTheForest(dynamic duck) 
    {
      duck.Quack();
      duck.Feathers();
    }

    private static void Game() 
    {
      Duck donald = new Duck();
      Person john = new Person();
      InTheForest(donald);
      InTheForest(john);
    }

    private static void Main() 
    {
      Game();
    }
  }
}

解决方案

Duck typing allows an object to be passed in to a method that expects a certain type even if it doesn’t inherit from that type. All it has to do is support the methods and properties of the expected type in use by the method. I emphasize that last phrase for a reason. Suppose we have a method that takes in a duck instance, and another method that takes in a rabbit instance. In a dynamically typed language that supports duck typing, I can pass in my object to the first method as long as my object supports the methods and properties of duck in use by that method. Likewise, I can pass my object into the second method as long as it supports the methods and properties of rabbit called by the second method. Is my object a duck or is it a rabbit? Like the above image, it’s neither and it’s both. In many (if not most) dynamic languages, my object does not have to support all methods and properties of duck to be passed into a method that expects a duck. Same goes for a method that expects a rabbit.It only needs to support the methods and properties of the expected type that are actually called by the method.

Please refer this to get an idea about Duck Typing

http://haacked.com/archive/2007/08/19/why-duck-typing-matters-to-c-developers.aspx/

这篇关于什么是接口鸭打字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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