我尝试通过继承两个类来创建myPet,但是出现错误,例如:
import UIKit class SecondViewController: UIViewController, UITextFieldDelegate { // No Error }
然后定义了以下类,然后创建新的类myPets,我想同时继承Dog和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不支持多重继承,遵循ObjectiveC。这不是从两个类继承:
class SecondViewController: UIViewController, UITextFieldDelegate
它是从一类继承UIViewController而采用的UITextFieldDelegate协议。在https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html上阅读有关协议的信息。
UIViewController
UITextFieldDelegate