Swift 方法
在本教程中,我们将通过示例了解 Swift 方法及其类型。
在类中定义的 Swift 函数称为方法。例如,
class Person {. . .// define methodsfunc greet() {// method body}}
这里,greet() 是在 Person 类中定义的方法。在学习方法之前,请确保了解 Swift 中 类 和 结构 的工作方式。
实例: 类方法
class Person {// define a methodfunc greet() {print("Hey there!")}}var nick = Person()// call methodnick.greet()
结果为:
Hey there!
在上面的例子中,我们在 Person 类中创建了一个名为 greet() 的方法。在这里,我们使用了类的对象 nick 来调用该方法。
// call methodnick.greet()
实例:使用 Swift 方法计算面积和体积
// create a classclass Hall {var length = 0.0var breadth = 0.0var height = 0.0// method to calculate areafunc calculateArea() {print("Area of Hall =", length * breadth)}// method to calculate volumefunc calculateVolume() {print("Volume of Hall =", length * breadth * height)}}// create object of Hall classvar hall1 = Hall()hall1.length = 42.5hall1.breadth = 30.8hall1.height = 45.2// call calculateArea() methodhall1.calculateArea()// call calculateVolume() methodhall1.calculateVolume()
结果为:
Area of Room = 1309.0Volume of Room = 59166.8
在上面的实例中,我们使用两个方法创建了一个名为 Hall 的类:
calculateArea()- 计算大厅的面积calulateVolume()- 计算大厅的体积
因为这些方法是在类内声明的,所以我们使用类的对象 hall1 来调用它们。
hall1.calculateArea()hall1.calculateVolume()
Swift 静态方法
在前面的实例中,我们使用了类的对象来访问其方法。但是,我们也可以创建无需创建对象即可访问的方法。
这些类型的方法称为静态方法。在 Swift 中,我们使用 static 关键字创建静态方法。例如,
class Calculator {// static methodstatic func add() {...}}
这里,add() 是静态方法。
要访问静态方法,我们使用类名。例如,
// access static methodCalculator.add()
实例:Swift 静态方法
class Calculator {// non-static methodfunc multiply(num1: Int, num2: Int) -> Int {return num1 * num2}// static methodstatic func add(num1: Int, num2: Int) -> Int {return num1 + num2}}// create an instance of the Calculator classvar obj = Calculator()// call static methodvar result2 = Calculator.add(num1: 2, num2: 3)print("2 + 3 =", result2)// call non-static methodvar result1 = obj.multiply(num1:2,num2:2)print("2 * 2 =", result1)
结果为:
2 * 2 = 42 + 3 = 5
在上面的实例中,我们创建了一个名为 Calculator 的类。该类包含静态方法:add() 和非静态方法 - multiply()
Calculator.add()- 使用类名调用静态方法multiply()- 使用类的对象调用非静态方法。
静态方法是类类型(与类而不是对象相关联),因此我们可以使用类名访问它们。
Swift 方法中的自身属性
有时,属性的名称和方法参数的名称可能相同。例如,
var physics = 0func checkEligibility(physics: Int) {...}
这里,property 和 method 参数具有相同的名称 physics。
在这种情况下,名称之间可能存在歧义。为了区分它们,我们使用了 self 属性。self 属性是指方法的当前对象。
实例:swift 自属性
class Marks {var physics = 0func checkEligibility(physics: Int) {// using self propertyif (self.physics < physics) {print("Not Eligible for admission")}else {print("Eligible for admission")}}}var student1 = Marks()student1.physics = 28student1.checkEligibility(physics: 50)
结果为:
Not eligible for admission
在上面的例子中,我们对属性和方法参数使用了相同的名称 physics。
为了区分它们,我们在 checkEligibility() 方法中使用了 self 属性。
if (self.physics < physics) {...}
这里,
self.physics-指值为 28 的student1对象的属性。physics- 指值为 50 的方法参数。
由于条件(28<50)为 true,因此执行 if 中的语句。
Swift mutating 方法
在 Swift 中,如果我们在类或结构中声明属性,则无法在方法中修改它们。例如,
struct Employee {var salary = 0.0...func salaryIncrement() {// Error Codesalary = salary * 1.5}}
在这里,由于 struct 是一种值类型,如果我们试图修改 salary 的值,我们将得到一条错误消息。
然而,如果我们想从方法内部修改值类型的属性,我们需要在声明方法时使用 mutating 关键字。
实例:从方法修改值类型
struct Employee {var salary = 0// define mutating functionmutating func salaryIncrement(increase: Int) {// modify salary propertysalary = salary + increaseprint("Increased Salary:",salary)}}var employee1 = Employee()employee1.salary = 20000employee1.salaryIncrement(increase: 5000)
结果如下:
Increased Salary: 25000
在上面的实例中,我们创建了一个名为 Employee 的结构。注意,
mutating func salaryIncrement(increase: Int) {// modify salary propertysalary = salary + increase...}
这里,mutating 方法让我们可以修改方法中的 salary 值。