本篇纯属抬杠之作,之前我们提到了Swift的泛型Protocol使用associatedtype关键字,而不是使用<Type>语法的泛型参数。这其中有什么好处呢?

  我就这个问题搜索了一些回答,大体上提到两点:

  <Type>语法对Protocol没有意义,Protocol仅需要定义一个抽象的概念,具体的类型应该由实现的Class来明确,比如:

ClassWithInt<Int>: NumberProtocol
ClassWithDouble<Double>: NumberProtocol

  associatedtype可以用来给Protocol中特定Func添加泛型约束,而不是限定整个Protocol

protocol GeneratorType {
associatedtype Element
public mutating func next() -> Self.Element?
}

  听上去还是有一定道理的,然后实践是检验事实的唯一标准。下面我们通过代码实例来和C#进行对比。首先拿出网上多被引用解释上述两个观点的Swift代码:

public protocol Automobile {
    associatedtype FuelType
    associatedtype ExhaustType
    func drive(fuel: FuelType) -> ExhaustType
}
public protocol Fuel {
    associatedtype ExhaustType
    func consume() -> ExhaustType
}
public protocol Exhaust {
    init()
    func emit()
} public struct UnleadedGasoline<E: Exhaust>: Fuel {
    public func consume() -> E {
        print("...consuming unleaded gas...")
        return E()
    }
}
public struct CleanExhaust: Exhaust {
    public init() {}
    public func emit() {
        print("...this is some clean exhaust...")
    }
}
public class Car<F: Fuel,E: Exhaust>: Automobile where F.ExhaustType == E {
    public func drive(fuel: F) -> E {
        return fuel.consume()
    }
} public class Car1<F: Fuel>: Automobile {
    public func drive(fuel: F) -> F.ExhaustType {
        return fuel.consume()
    }
}

  具体的使用情况如下:

var car = Car<UnleadedGasoline<CleanExhaust>, CleanExhaust>()
car.drive(fuel: UnleadedGasoline<CleanExhaust>()).emit() var fusion = Car1<UnleadedGasoline<CleanExhaust>>()
fusion.drive(fuel: UnleadedGasoline<CleanExhaust>()).emit()

  转换成C#代码的话,有两种思路,首先是把泛型参数放到Interface层面:

    public interface Automobile<FuelType, ExhaustType>
{
ExhaustType Drive(FuelType fuel);
}
public interface Fuel<ExhaustType>
{
ExhaustType consume();
}
public interface Exhaust
{
void Emit();
} public class UnleadedGasoline<Exhaust> : Fuel<Exhaust> where Exhaust : new()
{
public Exhaust consume()
{
Console.WriteLine("...consuming unleaded gas...");
return new Exhaust();
}
}
public class CleanExhaust : Exhaust
{
public void Emit()
{
Console.WriteLine("...this is some clean exhaust...");
}
}
public class Car : Automobile<UnleadedGasoline<CleanExhaust>, CleanExhaust>
{
public CleanExhaust Drive(UnleadedGasoline<CleanExhaust> fuel)
{
return fuel.consume();
}
}

  还可以模仿Swift对Automobile多做一层继承进行包装:

    public interface Car1<T1> : Automobile<UnleadedGasoline<T1>, T1> where T1 : new()
{ } public class SimpleCar : Car1<CleanExhaust>
{
public CleanExhaust Drive(UnleadedGasoline<CleanExhaust> fuel)
{
return fuel.consume();
}
}

调用的时候没有什么太大的差别:

  var gaso = new UnleadedGasoline<CleanExhaust>();
  var car = new Car();
  car.Drive(gaso).Emit();   var simpleCar = new SimpleCar();
  simpleCar.Drive(gaso).Emit();

  和Swift比较不同的是,我们在Interface就代入了泛型参数。但是由于我们不能直接实例化Interface,所以并不能直接使用Automobile来减少一层继承关系。

  因为上述提到的使用associatedtype 的第一点理由见仁见智,这里不分高下。

  C#还有第二种思路,就是我也把泛型约束下放到Func层级:

    public interface Automobile
{
ExhaustType Drive<FuelType,ExhaustType>(FuelType fuel) where ExhaustType : new();
}
public interface Fuel
{
ExhaustType consume<ExhaustType>() where ExhaustType : new();
} public class UnleadedGasoline : Fuel
{
public Exhaust consume<Exhaust>() where Exhaust : new()
{
Console.WriteLine("...consuming unleaded gas...");
return new Exhaust();
}
} public class Car2 : Automobile
{
public CleanExhaust Drive<UnleadedGasoline, CleanExhaust>(UnleadedGasoline fuel) where CleanExhaust : new()
{
return (fuel as Fuel).consume<CleanExhaust>();
}
}

C#的接口并不能定义构造函数。强行模仿起来还真是有点累啊。最终的使用也很简单:

    var fuel = new UnleadedGasoline();
var car2 = new Car2();
car2.Drive<UnleadedGasoline,CleanExhaust>(fuel).Emit();

  通篇比较下来,应该说Swift通过associatedtype 关键字和<Type>的混用,使得泛型的定义更为复杂也更灵活了。

  GitHub:

https://github.com/manupstairs/LearnSwift

https://github.com/manupstairs/LearnDotNetCore

Swift泛型Protocol对比C#泛型Interface的更多相关文章

  1. swift:入门知识之泛型

    在尖括号里写一个名字来创建一个泛型函数或者类型 例如<T>.<Type> 可以创建泛型类.枚举和结构体 在类型后使用where来指定一个需求列表.例如,要限定实现一个协议的类型 ...

  2. Java泛型和集合之泛型介绍

    在声明一个接口和类的时候可以使用尖括号带有一个或者多个参数但是当你在声明属于一个接口或者类的变量的时候或者你在创建一个类实例的时候需要提供他们的具体类型.我们来看下下面这个例子 List<Str ...

  3. Java基础----Java---集合框架---泛型、泛型方法、静态方法泛型、泛型接口、泛型限定、泛型类

    泛型:jdk1.5后的新特性,用于解决安全问题,是一个安全机制. 好处: 1.将运行时的异常出现问题classcastException.转移到了编译时期.方便程序员调试解决问题,让运行事情问题减少, ...

  4. Java泛型三:Java泛型详解

    原文地址https://www.cnblogs.com/lzq198754/p/5780426.html 1.为什么需要泛型 泛型在Java中有很重要的地位,网上很多文章罗列各种理论,不便于理解,本篇 ...

  5. Java泛型四:Java泛型总结

    原文地址https://www.cnblogs.com/lwbqqyumidi/p/3837629.html 一. 泛型概念的提出(为什么需要泛型)? 首先,我们看下下面这段简短的代码: 1 publ ...

  6. 牛客网Java刷题知识点之泛型概念的提出、什么是泛型、泛型在集合中的应用、泛型类、泛型方法、泛型接口、泛型限定上限、泛型限定下限、 什么时候使用上限?泛型限定通配符的体现

    不多说,直接上干货! 先来看个泛型概念提出的背景的例子. GenericDemo.java package zhouls.bigdata.DataFeatureSelection; import ja ...

  7. java 反射和泛型-反射来获取泛型信息

    通过指定对应的Class对象,程序可以获得该类里面所有的Field,不管该Field使用private 方法public.获得Field对象后都可以使用getType()来获取其类型. Class&l ...

  8. Java 泛型 协变式覆盖和泛型重载

    Java 泛型 协变式覆盖和泛型重载 @author ixenos 1.协变式覆盖(Override) 在JDK 1.4及以前,子类方法如果要覆盖超类的某个方法,必须具有完全相同的方法签名,包括返回值 ...

  9. java泛型基础、子类泛型不能转换成父类泛型

    参考http://how2j.cn/k/generic/generic-generic/373.html 1.使用泛型的好处:泛型的用法是在容器后面添加<Type>Type可以是类,抽象类 ...

随机推荐

  1. VS2012配置使用ICE通信接口

    1.下载安装Ice-3.5.1.msi http://www.zeroc.com/download.html 2.添加安装路径到环境变量path,D:\Program Files (x86)\Zero ...

  2. 在英文版操作系统中安装的MS SQL server,中文字段无法匹配

    在英文版的操作系统中安装的MS SQL server,会出现中文字段无法被匹配到.其原因在于英文环境下安装的MS SQL server的排序规则不包括中文. 所以解决办法就是更改MS SQL serv ...

  3. 【C语言学习】《C Primer Plus》第1章 概览

    学习总结 1.C语言于1972年由贝尔实验室的Dennis Ritchie在与Ken Thompson一起设计UNIX操作系统的时候开发的.的的设计构想来源于Ken Thompson的B语言.Anyw ...

  4. JavaScript使用DeviceOne开发实战(二) 生成调试安装包

    生成调试安装包 首先需要说明的是,这个步骤并不是每次调试App都必须的,大部分情况生成一次调试安装包,安装到手机上之后就可以忽略整个这个步骤.因为调试安装包包含了很多原生组件,都是可以定制勾选的,如果 ...

  5. 从3D Touch 看 原生快速开发

    全新的按压方式苹果继续为我们带来革命性的交互:Peek和Pop,Peek 和 Pop 让你能够预览所有类型的内容,甚至可对内容进行操作,却不必真的打开它们.例如,轻按屏幕,可用 Peek 预览收件箱中 ...

  6. Nim教程【十】

    openarray类型 注意:openarray类型只能用于参数 固定大小的数组虽然性能不错,但过于呆板,使用取来不是很方便 对于一个方法来说,传入参数如果是一个数组,最好是不要限制数组的长度 也就是 ...

  7. 使用FiddlerCore来测试WebAPI

    大家在调试Web相关的API时,经常会用Fiddler来查看相关的请求,以及返回结果.当然你也可以尝试修改或者重复你的请求信息.本文主要介绍如何使用代码来实现fiddler的功能. Fiddler C ...

  8. CSS hack前传——背景图片全屏

    在之前的博客CSS hack中我有提到,一个问题的解决让我对CSS hack的态度从不屑一顾,到认真研究了实验一下,事情是这样的,最近产品发布,向来狂妄的我被一个bug纠缠住了,甚至丧气的表示我做不出 ...

  9. Linux常用命令(转)

    源自:http://www.linuxidc.com/Linux/2011-08/40437.htm Linux管理文件和目录的命令 命令 功能 命令 功能 pwd 显示当前目录 ls 查看目录下的内 ...

  10. Redis笔记,安装和常用命令

    转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/96.html?1455870708 一.redis简单介绍 redis是N ...