Swift方法

在Swift 4语言中,与特定类型关联的函数称为“方法”。 在Objective C中,类用于定义方法,而Swift 4语言为用户提供了为类,结构体和枚举也提供方法,提高了灵活性。

实例方法

在Swift 4语言中,通过实例方法访问类,结构体和枚举实例。

实例方法提供功能 -

  • 访问和修改实例属性
  • 与实例需求相关的功能

可以在{}花括号内写入实例方法。 它具有对类型实例的方法和属性的隐式访问。 当调用该类型的特定实例时,它将访问特定实例。

语法

func funcname(Parameters) -> returntype {
   Statement1
   Statement2
   ---
   Statement N
   return parameters
}

示例代码

class calculations {
   let a: Int
   let b: Int
   let res: Int

   init(a: Int, b: Int) {
      self.a = a
      self.b = b
      res = a + b
   }

   func tot(c: Int) -> Int {
      return res - c
   }

   func result() {
      print("Result is: \(tot(c: 20))")
      print("Result is: \(tot(c: 50))")
   }
}
let pri = calculations(a: 600, b: 300)
pri.result()

当使用playground 运行上述程序时,得到以下结果 -

Result is: 880
Result is: 850

Calculations类定义了两个实例方法 -

  • init()方法定义为添加两个数字ab并将其存储在结果res
  • tot()方法用于从传递c值中减去res

最后,使用ab的值打印calculations方法。 使用点语法.访问实例方法。

局部和外部参数名称

Swift 4函数描述了它们变量的局部声明和全局声明。 类似地,Swift 4方法命名约定也类似于Objective C的命名约定。但是函数和方法的局部和全局参数名称声明的特征是不同的。 Swift 4中的第一个参数由介词名称引用为:withforby,以便于访问命名约定。

Swift 4通过将第一个参数名称声明为局部参数名称,而其余参数名称为全局参数名称,提供了方法的灵活性。 这里no1由Swift 4方法声明为局部参数名称。 no2用于全局声明并通过程序访问。

class division {
   var count: Int = 0
   func incrementBy(no1: Int, no2: Int) {
      count = no1 / no2
      print(count)
   }
}

let counter = division()
counter.incrementBy(no1: 1800, no2: 3)
counter.incrementBy(no1: 1600, no2: 5)
counter.incrementBy(no1: 11000, no2: 3)

当使用playground 运行上述程序时,得到以下结果 -

600
320
3666

带#和_符号的外部参数名称

即使Swift 4方法为本地声明提供了第一个参数名称,用户也可以修改从局部声明到全局声明的参数名称。 这可以通过在#符号前加上第一个参数名称来完成。 通过这样做,可以在整个模块中全局访问第一个参数。

当用户需要使用外部名称访问后续参数名称时,将使用_符号覆盖方法名称。

class multiplication {
   var count: Int = 0
   func incrementBy(no1: Int, no2: Int) {
      count = no1 * no2
      print(count)
   }
}

let counter = multiplication()

counter.incrementBy(no1: 800, no2: 3)
counter.incrementBy(no1: 100, no2: 5)
counter.incrementBy(no1: 15000, no2: 3)

当使用playground 运行上述程序时,得到以下结果 -

2400
500
45000

self属性

方法对所有已定义的类型实例都有一个self的隐式属性。Self属性用于引用其定义方法的当前实例。参考以下示例代码 -

class calculations {
   let a: Int
   let b: Int
   let res: Int

   init(a: Int, b: Int) {
      self.a = a
      self.b = b
      res = a + b
      print("Inside Self Block: \(res)")
   }

   func tot(c: Int) -> Int {
      return res - c
   }

   func result() {
      print("Result is: \(tot(c: 20))")
      print("Result is: \(tot(c: 50))")
   }
}

let pri = calculations(a: 600, b: 300)
let sum = calculations(a: 1200, b: 300)

pri.result()
sum.result()

当使用playground 运行上述程序时,得到以下结果 -

Inside Self Block: 900
Inside Self Block: 1500
Result is: 880
Result is: 850
Result is: 1480
Result is: 1450

从实例方法修改值类型

在Swift 4中,语言结构和枚举属于值类型,不能通过其实例方法进行更改。 但是,Swift 4语言提供了通过“变异”行为修改值类型。 mutating将在实例方法中进行任何更改,并在执行方法后返回到原始形式。 此外,通过self属性,为隐式函数创建新实例,并在执行后替换现有方法。

参考以下示例代码 -

struct area {
   var length = 1
   var breadth = 1

   func area() -> Int {
      return length * breadth
   }
   mutating func scaleBy(res: Int) {
      length *= res
      breadth *= res
      print(length)
      print(breadth)
   }
}

var val = area(length: 3, breadth: 5)
val.scaleBy(res: 3)
val.scaleBy(res: 30)
val.scaleBy(res: 300)

当使用playground 运行上述程序时,得到以下结果 -

9
15
270
450
81000
135000

变异方法的self属性

变异方法与self属性相结合,为定义的方法分配一个新实例。参考以下示例代码 -

struct area {
   var length = 1
   var breadth = 1
   func area() -> Int {
      return length * breadth
   }
   mutating func scaleBy(res: Int) {
      self.length *= res
      self.breadth *= res
      print(length)
      print(breadth)
   }
}

var val = area(length: 3, breadth: 5)
val.scaleBy(res: 13)

当使用playground 运行上述程序时,得到以下结果 -

39
65

类型方法

当调用方法的特定实例时,它被称为实例方法; 当方法调用特定类型的方法时,它被称为“类型方法”。 “类”的类型方法由func关键字定义,结构体和枚举类型方法在func关键字之前使用static关键字定义。

类型方法由操作符.调用和访问。 而不是调用特定实例来调用这个方法。

class Math {
   class func abs(number: Int) -> Int {
      if number < 0 {
         return (-number)
      } else {
         return number
      }
   }
}

struct absno {
   static func abs(number: Int) -> Int {
      if number < 0 {
         return (-number)
      } else {
         return number
      }
   }
}

let no = Math.abs(number: -35)
let num = absno.abs(number: -5)

print(no)
print(num)

当使用playground 运行上述程序时,得到以下结果 -

35
5

上一篇:Swift属性

下一篇:Swift下标

关注微信小程序
程序员编程王-随时随地学编程

扫描二维码
程序员编程王

扫一扫关注最新编程教程