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. 优雅的在React项目中使用Redux

    概念 首先我们会用到哪些框架和工具呢? React UI框架 Redux 状态管理工具,与React没有任何关系,其他UI框架也可以使用Redux react-redux React插件,作用:方便在 ...

  2. 找了两个node+vue-cli+mogodb的小项目学学

    实现的博客系统,自己抽时间搭建看看 https://www.cnblogs.com/yesyes/p/7188331.html https://github.com/chaohangz/vueBlog

  3. Windows 7 蓝屏代码大全 &amp; 蓝屏全攻略

    关于Windows 7.Vista等系统的蓝屏.之前软媒在Win7之家和Vista之家都有非常多文章讨论过,可是都是筛选的常见的一些问题,今天这个文章是个大全.希望大家看着别头痛.文章收藏下来以后待查 ...

  4. ZOJ 3684 Destroy 树的中心

    中心节点就是树的中心,2遍dfs求到树的直径.而中心一定在直径上,顺着直径找到中心就够了. 然后能够一遍树形DP找到最小值或者二分+推断是否訪问到叶子节点. #include <iostream ...

  5. 编译自己的gcc

    1 编译gcc需要的依赖 gmp mpfr mpc isl binutils 将它们都安装在同一个目录下即可. 2 --disable-nls 将native language support关掉,只 ...

  6. 通过shell脚本批处理es数据

    #!/bin/sh [按照指定的域名-website集合,遍历各个域名,处理url] #指定待删除的变量集合 arr=(6.0) cur="`date +%Y%m%d%H%M%S`" ...

  7. Selenium Webdriver弹出框 微博分享的内容控制与结果生成

    browser.window_handles for i in ugc_url_l: js = 'window.location.href="{}"'.format(i) brow ...

  8. eventkeyboardmouse

    http://www.javascriptkit.com/jsref/eventkeyboardmouse.shtml

  9. 【Codevs 3115】高精度练习之减法

    http://codevs.cn/problem/3115/ 板子题~ // <H.cpp> - Sun Oct 9 12:58:23 2016 // This file is made ...

  10. A Go library implementing an FST (finite state transducer)——mark下

    https://github.com/couchbaselabs/vellum Building an FST To build an FST, create a new builder using ...