protocol CommonTrait: class {

func commonBehavior() -> String

}

extension CommonTrait {

func commonBehavior() -> String {

return "from protocol extension"

}

}

class CommonThing {

func say() -> String {

return "override this"

}

}

class ParentClass: CommonThing, CommonTrait {

override func say() -> String {

return commonBehavior()

}

}

class AnotherParentClass: CommonThing, CommonTrait {

override func say() -> String {

return commonBehavior()

}

}

class ChildClass: ParentClass {

override func say() -> String {

return super.say()

// it works if it calls `commonBehavior` here and not call `super.say()`, but I don't want to do that as there are things in the base class I don't want to have to duplicate here.

}

func commonBehavior() -> String {

return "from child class"

}

}

let child = ChildClass()

child.say() // want to see "from child class" but it's "from protocol extension”

实现链条缺失时会使用protocol的缺省实现。不能路由到真正的实现。

https://stackoverflow.com/questions/31795158/swift-2-protocol-extension-not-calling-overridden-method-correctly

Unfortunately protocols don't have such an dynamic behavior (yet).

But you can do that (with the help of classes) by implementing commonBehavior() in the ParentClass and overriding it in the ChildClass. You also need CommonThing or another class to conform to CommonTrait which is then the superclass of ParentClass:

swift protocol 与类继承结合时的bug的更多相关文章

  1. 项目里出现两个配置类继承WebMvcConfigurationSupport时,为什么只有一个会生效(源码分析)

    为什么我们的项目里出现两个配置类继承WebMvcConfigurationSupport时,只有一个会生效.我在网上找了半天都是说结果的,没有人分析源码到底是为啥,博主准备讲解一下,希望可以帮到大家! ...

  2. C++类继承--构造函数时先构造基类

    以下说明继承类函数构造时,先构造基类: 析构基类时,若没加上virtual,只析构基类,不析构派生类: 析构派生类时,同时会析构基类: 1. 基类析构函数有virtual #include <s ...

  3. 写一个类继承socket时遇到的问题(TCP)

    主要为题出在服务器端的accept()函数,他返回两个参数,一个套接字和一个客户端的ip和端口组成的元组. 问题就出在这个套接字这里,我们继承了socket这个类,这个套接字创建的时候是通过socke ...

  4. C++父子类继承时的隐藏、覆盖、重载

    存在父子类继承关系时,若有同名成员函数同时存在,会发生隐藏.覆盖和重载这几种情况.对于初学者也比较容易混淆,为此,我整理了一下我的个人看法,仅供参考.希望对大家理解有帮助,也欢迎指正. 1.父子类继承 ...

  5. C++//菱形继承 //俩个派生类继承同一个基类 //又有某个类同时继承俩个派生类 //成为 菱形继承 或者 钻石 继承//+解决

    1 //菱形继承 2 //俩个派生类继承同一个基类 3 //又有某个类同时继承俩个派生类 4 //成为 菱形继承 或者 钻石 继承 5 6 #include <iostream> 7 #i ...

  6. 【Swift学习】Swift编程之旅---继承(十七)

    在 Swift 中,继承是区分「类」与其它类型的一个基本特征.swift不支持多重继承.类可以调用和访问超类的方法,属性和subscripts下标,并且可以重写(override)这些方法,属性和附属 ...

  7. swift学习笔记之-继承

    //继承 import UIKit /* 继承(Inheritance): 1.一个类可以继承(inherit)另一个类的方法(methods).属性(properties)和其它特性.当一个类继承其 ...

  8. 《Swift Programming Language 》——Swift中怎样使用继承(Inheritance)

    一个类能够继承(inherit)还有一个类的方法(methods),属性(property)和其他特性.当一个类继承其他类时,继承类叫子类(subclass),被继承类叫超类(或父类,supercla ...

  9. swift 学习- 10 -- 类和结构体

    // '类和结构体' 是人们构建代码所使用的一种通用且灵活的构造体, 我们可以使用完全相同的语法规则来为 '类和结构体' 定义属性 (变量 和 常量) 和添加方法, 从而扩展 类和结构体 的功能 // ...

随机推荐

  1. Django打造大型企业官网(八)

    4.16.侧边栏标题和广告位布局完成 templates/news/index.html <div class="sidebar-wrapper"> <div c ...

  2. Spring+Quartz实现定时任务的配置方法(插曲)

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46376093 1.Scheduler的配置 <bean class=&quo ...

  3. ppt五种经典字体组合

    在做ppt中常常为使用哪种字体而头疼,如今将ppt的经典字体附上.希望对大家有帮助 五种经典的字体组合 标题字体 正文字体 使用场合 方正综艺简体 微软雅黑 课题汇报.咨询报告.学术研讨等正式场合 方 ...

  4. spring mvc +Mybatis3.1 整合的时候异常

    在使用Mybatis3.10+spring3.10+mybatis-spring-1.0.0集成,使用spring 时发生例如以下错误: 严重: Servlet.service() for servl ...

  5. 【Espruino】NO.13 蓝牙模块

    http://blog.csdn.net/qwert1213131/article/details/31830809 本文属于个人理解,能力有限.纰漏在所难免,还望指正! [小鱼有点电] [Espru ...

  6. the largest value you actually can transmit between the client and server is determined by the amount of available memory and the size of the communications buffers.

    the largest value you actually can transmit between the client and server is determined by the amoun ...

  7. cordova常用命令

    安装 cordova: npm install -g cordova 创建应用程序 cordova create hello com.example.hello HelloWorld 添加平台 cor ...

  8. [POI2012]FES-Festival

    https://www.zybuluo.com/ysner/note/1252538 题面 有一个数列\(\{a\}\).现给定多组限制,限制分成\(2\)类,第一类是\(a_x+1=a_y\),有\ ...

  9. gitlab gerrit jenkins CI/CD环境集成

    http://blog.csdn.net/williamwanglei/article/details/38498465

  10. 基于ELK的传感器数据分析练习

    目录 Sensor Data Analytics Application 数据构成 数据模型设计 Logstash配置 Kibana可视化 Sensor Data Analytics Applicat ...