Declaring classes that adopt protocols
Now, we will declare a class that specifies that it conforms to the ComicCharacter
protocol in its declaration in the Playground. Instead of specifying a superclass, the class declaration includes the name of the previously declared ComicCharacter
protocol after the class name (AngryDog
) and the colon (:
). We can read the class declaration as "the AngryDog
class conforms to the ComicCharacter
protocol."
However, the class doesn't implement any of the required properties and methods specified in the protocol, so it doesn't really conform to the ComicCharacter
protocol, as shown in the following code. The code file for the sample is included in the swift_3_oop_chapter_05_02
folder:
open class AngryDog: ComicCharacter ...
Get Swift 3 ObjectOriented Programming - Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.