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, HorseDeeJay> instance named silverParty and passes jake and silver as arguments. This way, we can create a party of dogs with a horse DJ, where Jake is the party leader, and Silver is the DJ. The code file for the sample is included in the swift_3_oop_chapter_06_10 folder:

    var silver = HorseDeeJay(name: "Silver") 
    var silverParty = PartyWithDeeJay<Dog, HorseDeeJay>(leader: jake, 
    deeJay: silver) 

The silverParty instance will only accept a Dog instance for all the arguments in which the class definition uses the generic type parameter named T. The following lines add the previously created three instances of Dog to the party by calling the add method. The code file for the sample is included in the swift_3_oop_chapter_06_10 folder:

    silverParty.add(member: duke) 
    silverParty.add(member: lady) 
    silverParty.add(member: dakota) 

The following lines call the dance method to make the DJ invite all the dogs to dance and then make them dance. Then, the code removes a member that isn't the party leader, votes on a new leader, and finally calls the sing method to make the DJ invite all the dogs to sing and then make them sing. The code file for the sample is included in the swift_3_oop_chapter_06_10 folder:

    silverParty.dance() 
    try silverParty.remove(member: duke) 
    try silverParty.voteLeader() 
    silverParty.sing() 

The following lines show the generated output after we run the added code. The lines include text with descriptions instead of the emoji icons:

Jake welcomes Duke: Wooooof
Jake welcomes Lady: Wooooof
Jake welcomes Dakota: Wooooof
My name is Silver. Let's Dance.
Multiple musical notes emoji icon
Dancer emoji icon
Jake dances /- - /-/
Duke dances /- - /-/
Lady dances /- - /-/
Dakota dances /- - /-/
Duke says goodbye to Jake: Woof Wooooof Grr
Jake says: Lady has been voted as our new party leader.
Lady dances /- - /-/
Time to sing!
Guitar emoji icon
Jake sings: Woof Woof Woof . Woof Woof . Woof . 
Lady sings: Woof Woof Woof . Woof Woof . Woof . 
Dakota sings: Woof Woof Woof . Woof Woof . Woof .

The following screenshot shows the Playground with the execution results, including the emoji icons:

Using a generic class with two generic type parameters

The following lines create a PartyWithDeeJay<Frog, HorseDeeJay> instance named silverAndFrogsParty and passes frog1 and silver as arguments. This way, we can create a party of frogs with a horse DJ, where Frog #1 is the party leader, and Silver is the DJ. The code file for the sample is included in the swift_3_oop_chapter_06_11 folder:

    var silverAndFrogsParty = PartyWithDeeJay<Frog, HorseDeeJay>
    (leader: frog1, deeJay: silver) 

The silverAndFrogsParty instance will only accept a Frog instance for all the arguments in which the class definition uses the generic type parameter named T. The following lines add the previously created two instances of Frog to the party by calling the add method. The code file for the sample is included in the swift_3_oop_chapter_06_11 folder:

    silverAndFrogsParty.add(member: frog2) 
    silverAndFrogsParty.add(member: frog3) 

The following lines call the dance method to make the DJ invite all the dogs to dance and then make them dance. Finally, the code calls the sing method to make the DJ invite all the dogs to sing and then make them sing. The code file for the sample is included in the swift_3_oop_chapter_06_11 folder:

    silverAndFrogsParty.dance() 
    silverAndFrogsParty.sing() 

The following lines show the generated output after we run the added code. The lines include text with descriptions instead of the emoji icons:

Frog #1 welcomes Frog #2: Croak
Frog #1 welcomes Frog #3: Croak
My name is Silver. Let's Dance.
Multiple musical notes emoji icon
Dancer emoji icon
Frog #1 dances /| |/ ^ ^ 
Frog #2 dances /| |/ ^ ^ 
Frog #3 dances /| |/ ^ ^ 
Time to sing!
Guitar emoji icon
Frog #1 sings: Ribbit Ribbit Ribbit . Ribbit Ribbit . Ribbit . 
Frog #2 sings: Ribbit Ribbit Ribbit . Ribbit Ribbit . Ribbit . 
Frog #3 sings: Ribbit Ribbit Ribbit . Ribbit Ribbit . Ribbit . 

The following screenshot shows the Playground with the execution results, including the emoji icons:

Using a generic class with two generic type parameters

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.75.59