Using a generic class with two generic type parameters
We can create instances of the PartyWithDeeJay<AnimalElement, DeeJayElement>
class by replacing both the AnimalElement
and DeeJayElement
generic type parameters with any type names that conform to the constraints specified in the declaration of the PartyWithDeeJay<AnimalElement, DeeJayElement>
class. We have three concrete classes that implement both the AnimalProtocol
and Equatable
protocols: Dog
, Frog
, and Lion
. We have one class that conforms to the DeeJayProtocol
protocol: HorseDeeJay
. Thus, we can use Dog
and HorseDeeJay
to create an instance of PartyWithDeeJay<Dog, HorseDeeJay>
.
The following lines create a HorseDeeJay
instance named silver
. Then, the code creates a PartyWithDeeJay<Dog, ...
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.