We can combine class inheritance with protocol conformance. The following lines show the code for a new AngryCatAlien
class that inherits from the AngryCat
class and conforms to the Alien
protocol. Note that the class declaration includes the superclass (AngryCat
) and the implemented protocol (Alien
) separated by a comma after the colon (:
):
public class AngryCatAlien : AngryCat, Alien {
public var numberOfEyes: Int = 0
init (nickName: String, age: UInt, fullName: String, initialScore: UInt, x: UInt, y: UInt, numberOfEyes: Int) {
super.init(nickName: nickName, age: age, fullName: fullName, initialScore: initialScore, x: x, y: y)
self.numberOfEyes = numberOfEyes
}
public func appear() {
print("I'm \(fullName) and you can see my \(numberOfEyes) eyes.")
}
public func disappear() {
print("\(fullName) disappears.")
}
}
As a result of the previous code, we have a new class named
AngryCatAlien...