A detailed explanation of Swift method of method in object oriented programming

  • 2020-05-17 06:38:57
  • OfStack


struct Point {
  var x:Double
  var y:Double
  mutating func move(x:Double,y:Double) {
    self = Point(x: self.x+x,y: self.y+y)
  }
  static func name(){
    print("Point")
  }
}
Point.name()

1. The introduction

Method is just a term, which is to combine functions with specific types. Class, structure and enumeration can all define methods, and methods can be divided into instance methods and type methods. Type methods are similar to class methods in Objective-C. The big difference between Swift and Objective-C is that Objective-C can only define methods in a class.

2. Example method basis

The syntax of the instance method is exactly the same as the function, which is associated with an instance of a specific type. The instance method is called by the instance point syntax of the type to complete some functional modules. Here's an example:


class Math {
  // An instance method that completes the addition function 
  func add(param1:Double,param2:Double)->Double{
    return param1+param2
  }
}
// Creating a type instance 
var obj = Math()
// Call the method to calculate 
obj.add(5, param2: 5)

Similar to Objective-C, every instance of Swift class contains a hidden self property. The self property is the instance itself. Developers can use self in instance methods to call properties or other instance methods.


class Math {
  // An instance method that completes the addition function 
  func add(param1:Double,param2:Double)->Double{
    return param1+param2
  }
  func mul(param1:Double,param2:Double) -> Double {
    // use self Calling instance methods 
    self.add(param1, param2: param2)
    return param1*param2
  }
}

However, Swift does not require developers to write self. By default, developers can simply omit self to call properties and methods:


class Math {
  // An instance method that completes the addition function 
  func add(param1:Double,param2:Double)->Double{
    return param1+param2
  }
  func mul(param1:Double,param2:Double) -> Double {
    // use self Calling instance methods 
    add(param1, param2: param2)
    return param1*param2
  }
}

In one case, if the parameter name in the method is the same as the property name of the class instance, self must be used to call the class instance property to avoid ambiguity:


class Math {
  var param1 = 10.0
  // An instance method that completes the addition function 
  func add(param1:Double,param2:Double)->Double{
    // I'm going to use param1=10 If you don't add self  The parameter will be used param1
    return self.param1+param2
  }
  func mul(param1:Double,param2:Double) -> Double {
    // use self Calling instance methods 
    add(param1, param2: param2)
    return param1*param2
  }
}

3. Modify the value of a value type in an instance method

First need to understand a concept, there are two types of Swift, value types and reference types, specifically in the class, structure, relevant introduction enumerated in section 1, it is important to note that for value types, namely, structure and enumeration, it doesn't directly in the instance method changes the value of the instance attribute Swift provides another 1 kind of way, if there is such demand, developers can use mutating keyword instance method statement into variable, in fact, if in the variable instance method changed the value type attribute's value, is creates a new instance instead of the original instance, Here's an example:


struct Point {
  var x:Double
  var y:Double
  mutating func move(x:Double,y:Double) {
    self.x+=x
    self.y+=y
  }
}
var point = Point(x: 1, y: 1)
print(point)
point.move(3, y: 3)
print(point)

To modify the value of a property in the variable method of a value type instance is to create a new instance. The above method and the following method are the same:


struct Point {
  var x:Double
  var y:Double
  mutating func move(x:Double,y:Double) {
    self = Point(x: self.x+x,y: self.y+y)
  }
}

4. Type method

Just as an instance method is called by an instance of a type, a type method is called directly by a type, self in a type method refers to the current type, as opposed to an instance method, and developers can use self to distinguish between type properties and parameters in a type method. Type method creation using the Static keyword:

If you are creating a type method in a class, you should use the class keyword if the method can be overridden by a subclass. Here is an example:


class Math {
  var param1 = 10.0
  // An instance method that completes the addition function 
  func add(param1:Double,param2:Double)->Double{
    // I'm going to use param1=10 If you don't add self  The parameter will be used param1
    return self.param1+param2
  }
  func mul(param1:Double,param2:Double) -> Double {
    // use self Calling instance methods 
    add(param1, param2: param2)
    return param1*param2
  }
  class func name(){
    print("Math")
  }
}
Math.name()


Related articles: