Swift-Protocol, Class extension (extension), access control (Fileprivate,private,internal,public,open)

Source: Internet
Author: User
Tags instance method modifier



1,swift protocol is similar to the interface in other languages, the Protocol only makes the declaration of methods, including the method name, return value, parameters and other information, and no specific method implementation.


Protocol person {    //read-write property    var nprotocol Person {
     // Read and write attributes
     var name: String {get set}
     // Read-only property
     var age: Int {get}
    
     // type method
     static func method1 ()
    
     // Instance method
     func method2 ()-> Int
    
     // mutation method
     mutating func method3 ()
    
}


2, the protocol can inherit another protocol


protocol Animal {
    func move()
}
protocol Bird:Animal{
    func song()
}
class Chiken: Bird {

    func song() {
        print("gege")
    }
    func move() {
        print("run")
    }
}


3, if a class set inherits a parent class and a protocol is followed, then the colon should be preceded by the parent class, and then the protocol is written


Class CC: Inherited parent class, protocol 1, Protocol 2{}




Class extension (extension) 1, adding computed properties to an existing class and evaluating static properties 2, defining a new instance method and class method 3, providing a new constructor 4, defining subscript script 5, is an existing type that conforms to a protocol individual modifier differences
    1. Properties or methods that are modified by the private access level can only be accessed in the current class.
    2. The properties or methods modified by the Fileprivate access level are accessible in the current Swift source file.
    3. Internal (default access level, internal modifier can be written without writing)
    4. The properties or methods that the internal access level modifies can be accessed throughout the module where the source code resides.
      • If it is a frame or library code, it is accessible throughout the framework and is not accessible when the framework is referenced by external code.
      • If it is an app code, it is also available throughout the app code and within the entire app.
    5. Public can be accessed by anyone. However, other module can not be override and inherit, and within module can be override and inherit.
    6. Open can be used by anyone, including override and inheritance





Swift-Protocol, Class extension (extension), access control (Fileprivate,private,internal,public,open)


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.