Inheriting and adding associated types in protocols

Now, we want to declare a PartyWithDeeJayProtocol protocol and make the generic PartyWithDeeJay<AnimalElement, DeeJayElement> class conform to this new protocol. We will make this protocol inherit from the previously created PartyProtocol that defined a MemberType associated type. Thus, the PartyWithDeeJayProtocol protocol will inherit this associated type. We have to specify another associated type that will be specified during the protocol implementation, that is, when we declare the class that conforms to the new protocol. The following lines show the declaration of the PartyWithDeeJayProtocol protocol that inherits from the PartyProtocol protocol. We must declare the protocol before the open class PartyWithDeeJay<AnimalElement: AnimalProtocol, DeeJayElement: DeeJayProtocol>: Party<AnimalElement> where AnimalElement: Equatable line that starts the declaration of the PartyWithDeeJay<AnimalElement, DeeJayElement> class that we want to edit to make it conform to this new protocol. The code file for the sample is included in the swift_3_oop_chapter_06_12 folder:

    public protocol PartyWithDeeJayProtocol: PartyProtocol { 
      associatedtype DeeJayType 
     
      init(leader: MemberType, deeJay: DeeJayType) 
    } 

The first line within the protocol body declares an associated type named DeeJayType. Then, the initializer requirement uses the inherited MemberType associatedtype and the new DeeJayType associatedtype to specify the types that the generic class conforming to this protocol will replace with the generic type parameter names.

The following code shows the first lines of the new declaration of the Party<AnimalElement, DeeJayElement> class that conforms to the recently created PartyWithDeeJayProtocol protocol. After the type constraints included within the angle brackets (< >) and the semicolon (:) followed by the class from which the class inherits, Party<AnimalElement>, the class declaration adds a comma (,), followed by the protocol to which the generic class conforms: PartyWithDeeJayProtocol. As we specified an initializer requirement in the PartyWithDeeJayProtocol protocol, we have to add public required as a prefix before the init declaration that receives an AnimalElement, leader, and a DeeJayElement, deeJay, as arguments. The rest of the code for the class remains without changes. The code file for the sample is included in the swift_3_oop_chapter_06_12 folder:

    open class PartyWithDeeJay<AnimalElement: AnimalProtocol, 
    DeeJayElement: DeeJayProtocol>: Party<AnimalElement>, 
    PartyWithDeeJayProtocol where AnimalElement: Equatable { 
      public var deeJay: DeeJayElement 
     
      public required init(leader: AnimalElement, 
      deeJay: DeeJayElement) { 
        self.deeJay = deeJay 
        super.init(leader: leader) 
      } 
 
      public required init(leader: AnimalElement) { 
        fatalError("init(leader:) has not been implemented") 
      } 
     
      open override func dance() { 
        deeJay.playMusicToDance() 
        super.dance() 
      } 
     
      open override func sing() { 
        deeJay.playMusicToSing() 
        super.sing() 
      } 
    } 
..................Content has been hidden....................

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