Difference between type method and type instance method, etc?

后端 未结 5 531
清歌不尽
清歌不尽 2020-12-23 15:04

Note: I\'ve read the apple documentation and studied a swift book.

  1. I\'m confused on the difference between a \"type instance method\" (if such exists, corre

5条回答
  •  执笔经年
    2020-12-23 15:47

    In Swift, types are either named types or compound types. Named types include classes, structures, enumerations, and protocols. In addition to user-defined named types, Swift defines many named types such as arrays, dictionaries, and optional values. (Let's ignore compound types for now since it doesn't directly pertain to your question.)

    To answer your questions, suppose that I create a user defined class called Circle (this is just an example):

    class Circle {
    
        static let PI = 3.14
    
        var radius: Double
    
        init(radius: Double) {
            self.radius = radius
        }
    
        // Returns the area of this circle
        func area() {
            return PI * radius
        }
    
        // Ridiculous class method for demonstration purposes
        static func printTypeName() {
            println("Circle")
        }
    } 
    
    1. I'm confused on the difference between a "type instance method" (if such exists, correct me if I'm wrong) and a type method?

    As mentioned earlier, a type refers to a class, structure, enumeration, protocol, and compound types. In my example above, I use a class called Circle to define a type.

    If I want to construct an individual object of the Circle class then I would be creating an instance. For example:

    let myCircleInstance = Circle(radius: 4.5)
    let anotherCircleInstance = Circle(radius: 23.1)
    

    The above are objects or instances of Circle. Now I can call instance methods on them directly. The instance method defined in my class is area.

    let areaOfMyCircleInstance = myCircleInstance.area()
    

    Now, a type method is a method that can be called directly on the type without creating an instance of that type.

    For example:

    Circle.printTypeName()
    

    Notice that there is a static qualifier before the func. This indicates that it pertains to the type directly and not to an instance of the type.

    1. Difference between class method and instance method?

    See the explanation above.

    1. Difference between type property and instance property(if such exists, sorry I'm very confused on Type Properties subject)?

    This is a similar explanation to the one in your question one except that instead of applying to methods, it applies to the properties (i.e., attributes, variables) of the type.

    In my Circle example, the properties are defined as:

    static let PI = 3.14
    var radius: Double
    

    The property PI is a type property; it may be accessed directly by the type

    Circle.PI
    

    The property radius is an instance property of the type; it may be accessed by an instance of the type. Using the variables we created earlier:

    // I can do this; it will be 4.5
    myCircleInstance.radius
    
    // And this; it will be 23.1
    anotherCircleInstance.radius
    
    // But I CANNOT do this because radius is an instance property!
    Circle.radius
    
    1. Lastly, Do class properties exist in swift?

    Absolutely! Read my explanation to your question 3 above. The PI property in my example is an example of a class property.

    References:

    • Swift Language Reference - Types
    • Swift Language Reference - Properties
    • Swift Language Reference - Methods

提交回复
热议问题