为什么我不能像库类一样从 swift 继承多个类 [英] Why I can not inherit from multiple classes in swift just like it's library classes

查看:26
本文介绍了为什么我不能像库类一样从 swift 继承多个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过从两个类继承来创建 myPet 但例如错误:

I try to create myPet by inherit from two classes but error for example:

import UIKit
class SecondViewController: UIViewController, UITextFieldDelegate {
    // No Error
}

然后定义以下类,然后创建新类 myPets,我喜欢继承 Dog 和 Substance.但是错误:来自类Dog"和Substance"的多重继承

Then the following classes were defined then create new class myPets which I like to inherit both Dog and Substance. But error: Multiple inheritance from classes 'Dog' and 'Substance'

class Dog:Animal {
    func sound()->String {
        return "Hong Hong"
    }
}

class Substance {
    func livingCompound()->String {
        return "Consist of bio-molecule"
    }
}

class myPets:Dog, Substance {
    func itsAddress()->String {
        // Error:Multiple inheritance from classes 'Dog' and 'Substance'
    }
}

推荐答案

Swift 不支持多重继承,在此遵循 Objective C.这不是继承自两个类:

Swift does not support multiple inheritance, following Objective C in this. This is NOT inheritance from two classes:

class SecondViewController: UIViewController, UITextFieldDelegate

继承自一类UIViewController,采用UITextFieldDelegate协议.在 https://developer.apple.com 上阅读协议/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html

It is inheritance from one class UIViewController and adopting the UITextFieldDelegate protocol. Read about protocols at https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html

这篇关于为什么我不能像库类一样从 swift 继承多个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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