Specifying requirements for methods
The AnimalProtocol
protocol requires two type methods: printALeg
and printAChild
. As explained with the type property requirements, we can only use the static
keyword to specify a type method requirement, but we can use either static
or class
when we implement the type method in the class that conforms to the protocol. The usage of the static
keyword doesn't have the same meaning that this keyword has when we use it in classes; that is, we can still declare type methods that can be overridden in the classes that conform to the protocol by declaring them with the class
keyword in the respective classes. The following line shows the type method requirement for printALeg
:
static func printALeg()
The protocol defines three parameterless methods: printLegs
, printChildren
, and printAge
. The method requirements use the func
keyword followed by the method name and its arguments, as if we were writing the method declaration for a class but without the...