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:
var jake = Dog(name: "Jake") var duke = Dog(name: "Duke") var lady = Dog(name: "Lady") var dakota = Dog(name: "Dakota") var dogsParty = Party<Dog>(leader: jake)
The dogsParty
instance will only accept a Dog
instance for...