Specifying requirements for methods

The AnimalProtocol protocol requires two type methods: printALeg and printAChild. As explained with the type property requirements, we can only use the static keyword to specify a type method requirement, but we can use either static or class when we implement the type method in the class that conforms to the protocol. The usage of the static keyword doesn't have the same meaning that this keyword has when we use it in classes; that is, we can still declare type methods that can be overridden in the classes that conform to the protocol by declaring them with the class keyword in the respective classes. The following line shows the type method requirement for printALeg:

    static func printALeg() 

The protocol defines three parameterless methods: printLegs, printChildren, and printAge. The method requirements use the func keyword followed by the method name and its arguments, as if we were writing the method declaration for a class but without the method body. The following line shows the method requirement for printLegs:

    func printLegs() 

The following lines show the code that declares the DomesticProtocol protocol. The code file for the sample is included in the swift_3_oop_chapter_05_12 folder:

    public protocol DomesticProtocol { 
      var name: String { get set } 
      var favoriteToy: String { get set } 
      
      func talk() 
    } 

The DomesticProtocol protocol requires two read/write stored properties: name and favoriteToy. In addition, the protocol defines a method requirement for a parameterless talk instance method. Note that the DomesticProtocol protocol doesn't inherit from the AnimalProtocol protocol, so we can combine the conformance to other protocols with DomesticProtocol to create a specific domestic version.

The following lines show the code that declares the MammalProtocol protocol. The code file for the sample is included in the swift_3_oop_chapter_05_12 folder:

    public protocol MammalProtocol: AnimalProtocol { 
      var isPregnant: Bool { get set } 
    } 

The MammalProtocol protocol inherits from the AnimalProtocol protocol and just adds the requirement for a single read/write stored property: isPregnant.

The following lines show the code that declares the DogProtocol protocol. The code file for the sample is included in the swift_3_oop_chapter_05_12 folder:

    public protocol DogProtocol: MammalProtocol { 
      var breed: String { get } 
      var breedFamily: String { get } 
       
      func printBreed() 
      func printBreedFamily() 
      func bark() 
      func bark(times: Int) 
      func bark(times: Int, otherDomestic: DomesticProtocol) 
      func bark(times: Int, otherDomestic: DomesticProtocol, 
      isAngry: Bool) 
      func printBark(times: Int, otherDomestic: DomesticProtocol?, 
      isAngry: Bool) 
    } 

The DogProtocol protocol inherits from the MammalProtocol protocol and adds two read-only stored properties: breed and breedFamily. In addition, the protocol adds many method requirements. There are many overloaded method requirements with the same name (bark) and different arguments. Thus, the class or classes that implement the DogProtocol protocol must implement all the specified overloads for the bark method. Note that the otherDomestic argument is of a protocol type (DomesticProtocol), so any instance of a class that conforms to this protocol can be used as an argument.

The following lines show the code that declares the CatProtocol protocol. The code file for the sample is included in the swift_3_oop_chapter_05_12 folder:

    public protocol CatProtocol: MammalProtocol { 
      func printMeow(times: Int) 
    } 

The CatProtocol protocol inherits from the MammalProtocol protocol and adds a printMeow method requirement that receives a times Int argument.

The following lines show the code that declares the BirdProtocol protocol. The code file for the sample is included in the swift_3_oop_chapter_05_12 folder:

    public protocol BirdProtocol: AnimalProtocol { 
      var feathersColor: String { get set } 
    } 

The BirdProtocol protocol inherits from the AnimalProtocol protocol and adds a feathersColor read/write stored property requirement. However, wait; we said that we needed birds to talk and have a favorite toy. The BirdProtocol class doesn't include a requirement for either a talk method or a favoriteToy property, and it doesn't inherit. However, we will create a class that implements both the BirdProtocol and the DomesticProtocol protocols, and we will be able to use a domestic bird that talks as an argument in any method that requires DomesticProtocol.

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

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