O'Reilly logo

Object–Oriented Programming with Swift 2 by Gastón C. Hillar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using a generic class for multiple types

We can create instances of the Party<T> class by replacing the T generic type parameter with any type name that conforms to the constraints specified in the declaration of the Party<T> class. So far, we have three concrete classes that implement both the AnimalProtocol and Equatable protocols: Dog, Frog, and Lion. Thus, we can use Dog to create an instance of Party<Dog>—that is, a Party instance of Dog objects.

The following code shows the lines that create four instances of the Dog class: jake, duke, lady, and dakota. Then, the code creates a Party<Dog> instance named dogsParty and passes jake as the leader argument to the initializer. This way, we will create a party of dogs, and Jake is the party leader: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required