当前位置: 首页 > news >正文

塑胶加工东莞网站建设技术支持重庆建设机电有限公司网站

塑胶加工东莞网站建设技术支持,重庆建设机电有限公司网站,网络营销哪些公司好做呢,公司展示类网站模板免费下载本篇纯属抬杠之作#xff0c;之前我们提到了Swift的泛型Protocol使用associatedtype关键字#xff0c;而不是使用Type语法的泛型参数。这其中有什么好处呢#xff1f; 我就这个问题搜索了一些回答#xff0c;大体上提到两点#xff1a; Type语法对Protoco…   本篇纯属抬杠之作之前我们提到了Swift的泛型Protocol使用associatedtype关键字而不是使用Type语法的泛型参数。这其中有什么好处呢   我就这个问题搜索了一些回答大体上提到两点   Type语法对Protocol没有意义Protocol仅需要定义一个抽象的概念具体的类型应该由实现的Class来明确比如 ClassWithIntInt: NumberProtocol ClassWithDoubleDouble: NumberProtocolassociatedtype可以用来给Protocol中特定Func添加泛型约束而不是限定整个Protocol protocol GeneratorType {associatedtype Elementpublic mutating func next() - Self.Element? }听上去还是有一定道理的然后实践是检验事实的唯一标准。下面我们通过代码实例来和C#进行对比。首先拿出网上多被引用解释上述两个观点的Swift代码 public protocol Automobile {associatedtype FuelTypeassociatedtype ExhaustTypefunc drive(fuel: FuelType) - ExhaustType } public protocol Fuel {associatedtype ExhaustTypefunc consume() - ExhaustType } public protocol Exhaust {init()func emit() }public struct UnleadedGasolineE: 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 CarF: Fuel,E: Exhaust: Automobile where F.ExhaustType E {public func drive(fuel: F) - E {return fuel.consume()} }public class Car1F: Fuel: Automobile {public func drive(fuel: F) - F.ExhaustType {return fuel.consume()} }具体的使用情况如下 var car CarUnleadedGasolineCleanExhaust, CleanExhaust() car.drive(fuel: UnleadedGasolineCleanExhaust()).emit()var fusion Car1UnleadedGasolineCleanExhaust() fusion.drive(fuel: UnleadedGasolineCleanExhaust()).emit()转换成C#代码的话有两种思路首先是把泛型参数放到Interface层面 public interface AutomobileFuelType, ExhaustType{ExhaustType Drive(FuelType fuel);}public interface FuelExhaustType{ExhaustType consume();}public interface Exhaust {void Emit();}public class UnleadedGasolineExhaust : FuelExhaust 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 : AutomobileUnleadedGasolineCleanExhaust, CleanExhaust{public CleanExhaust Drive(UnleadedGasolineCleanExhaust fuel){return fuel.consume();}}   还可以模仿Swift对Automobile多做一层继承进行包装 public interface Car1T1 : AutomobileUnleadedGasolineT1, T1 where T1 : new(){}public class SimpleCar : Car1CleanExhaust{public CleanExhaust Drive(UnleadedGasolineCleanExhaust fuel){return fuel.consume();}} 调用的时候没有什么太大的差别   var gaso new UnleadedGasolineCleanExhaust();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 DriveFuelType,ExhaustType(FuelType fuel) where ExhaustType : new();}public interface Fuel{ExhaustType consumeExhaustType() where ExhaustType : new();}public class UnleadedGasoline : Fuel{public Exhaust consumeExhaust() where Exhaust : new(){Console.WriteLine(...consuming unleaded gas...);return new Exhaust();}}public class Car2 : Automobile{public CleanExhaust DriveUnleadedGasoline, CleanExhaust(UnleadedGasoline fuel) where CleanExhaust : new(){return (fuel as Fuel).consumeCleanExhaust();}} C#的接口并不能定义构造函数。强行模仿起来还真是有点累啊。最终的使用也很简单 var fuel new UnleadedGasoline();var car2 new Car2();car2.DriveUnleadedGasoline,CleanExhaust(fuel).Emit();   通篇比较下来应该说Swift通过associatedtype 关键字和Type的混用使得泛型的定义更为复杂也更灵活了。   GitHub https://github.com/manupstairs/LearnSwift https://github.com/manupstairs/LearnDotNetCore     转载于:https://www.cnblogs.com/manupstairs/p/5980850.html
http://www.zqtcl.cn/news/163306/

相关文章:

  • 书店网站的建设网络营销方法有哪些
  • 深圳网站优化软件顺企网怎么样
  • 做网站的需要什么要求中国五百强企业排名表
  • 网络营销 企业网站外贸响应式网站建设
  • 网站网页制作公司o2o平台是什么意思啊
  • 惠州市网站建设个人网站怎么进入后台维护
  • 微信网站链接怎么做wordpress 绑定手机版
  • 网站建设的内容是什么在线阅读小说网站怎么建设
  • 福州网站开发哪家比较好建设网站需要掌握什么编程语言
  • 邹平做网站的公司莱芜人才网莱芜招聘
  • 旅行网站开发意义怎样优化网络速度
  • 手机微网站建设多少钱拟定网络设计方案
  • 厦门制作公司网站安卓原生app开发工具
  • worldpress英文网站建设wordpress输出外部文章
  • u9u8网站建设商业公司的域名
  • 有学给宝宝做衣服的网站吗防网站黑客
  • 十大搜索引擎网站微信小程序有什么用处?
  • 团购网站 seo烟台网站建设方案优化
  • 公司网站建设招标文件范本公益永久免费主机
  • 建设银行网站查询企业年金五合一免费建站
  • 做网站开发挣钱吗做网站手机版
  • 网站建设案例精粹 电子书广州白云学校网站建设
  • 良品铺子网站制作用什么软件来做网站
  • ip直接访问网站 备案哪有深圳设计公司
  • 平面构成作品网站第一设计
  • 济南小程序开发多少钱网站移动端优化工具
  • 大连开发区网站淘宝网站优化实例
  • 张家港建网站的公司做网站犯法了 程序员有责任吗
  • 小型企业网站建设项目浦东新区网站推广公司
  • 上海做网站优化公司ps最好用的素材网站