在类中定义的 Swift 函数称为方法。例如,
class Person {
. . .
// define methods
func greet() {
// method body
}
}
这里,greet() 是定义在 Person 类中的一个方法。在学习方法之前,请确保您了解 Swift 中的 类和 结构体 的工作原理。
示例 1:Swift 方法
class Person {
// define a method
func greet() {
print("Hey there!")
}
}
var nick = Person()
// call method
nick.greet()
输出
Hey there!
在上面的示例中,我们在 Person 类中创建了一个名为 greet() 的方法。这里,我们使用了该类的对象 nick 来调用该方法。
// call method
nick.greet()
greet() 方法在其中简单地打印字符串 "Hey there!"。
示例 2:使用 Swift 方法计算面积和体积
// create a class
class Hall {
var length = 0.0
var breadth = 0.0
var height = 0.0
// method to calculate area
func calculateArea() {
print("Area of Hall =", length * breadth)
}
// method to calculate volume
func calculateVolume() {
print("Volume of Hall =", length * breadth * height)
}
}
// create object of Hall class
var hall1 = Hall()
hall1.length = 42.5
hall1.breadth = 30.8
hall1.height = 45.2
// call calculateArea() method
hall1.calculateArea()
// call calculateVolume() method
hall1.calculateVolume()
输出
Area of Room = 1309.0 Volume of Room = 59166.8
在上面的示例中,我们创建了一个名为 Hall 的类,其中包含两个方法:
calculateArea()- 计算大厅的面积calulateVolume()- 计算大厅的体积
由于这些方法是在类中声明的,因此我们使用了该类的对象 hall1 来调用它们。
hall1.calculateArea()
hall1.calculateVolume()
Swift 静态方法
在前面的示例中,我们使用类的对象来访问其方法。但是,我们也可以创建无需创建对象即可访问的方法。
这些类型的方法称为静态方法。在 Swift 中,我们使用 static 关键字来创建静态方法。例如,
class Calculator {
// static method
static func add() {
...
}
}
这里,add() 是静态方法。
要访问静态方法,我们使用类名。例如,
// access static method
Calculator.add()
示例:Swift 静态方法
class Calculator {
// non-static method
func multiply(num1: Int, num2: Int) -> Int {
return num1 * num2
}
// static method
static func add(num1: Int, num2: Int) -> Int {
return num1 + num2
}
}
// create an instance of the Calculator class
var obj = Calculator()
// call static method
var result2 = Calculator.add(num1: 2, num2: 3)
print("2 + 3 =", result2)
// call non-static method
var result1 = obj.multiply(num1:2,num2:2)
print("2 * 2 =", result1)
输出
2 * 2 = 4 2 + 3 = 5
在上面的示例中,我们创建了一个名为 Calculator 的类。该类包含静态方法:add() 和非静态方法 - multiply()
这里,
Calculator.add()- 使用类名调用静态方法obj.multiply()- 使用类的对象调用非静态方法。
静态方法是类类型(与类相关联而非对象),因此我们可以使用类名访问它们。
注意:类似地,我们也可以在结构体中创建静态方法。结构体内的静态方法是结构体类型,因此我们使用结构体名称来访问它们。
Swift 方法中的 self 属性
有时,属性的名称和方法的参数名称可能相同。例如,
var physics = 0
func checkEligibility(physics: Int) {
...
}
这里,属性和方法参数都具有相同的名称 physics。
在这种情况下,名称之间可能会存在歧义。因此,为了区分它们,我们使用 self 属性。self 属性引用方法当前的对象。
示例:Swift self 属性
class Marks {
var physics = 0
func checkEligibility(physics: Int) {
// using self property
if (self.physics < physics) {
print("Not Eligible for admission")
}
else {
print("Eligible for admission")
}
}
}
var student1 = Marks()
student1.physics = 28
student1.checkEligibility(physics: 50)
输出
Not eligible for admission
在上面的示例中,我们将属性和方法参数都命名为 physics。
为了区分它们,我们在 checkEligibility() 方法中使用了 self 属性。
if (self.physics < physics) {
...
}
这里,
- self.physics - 引用
student1对象的属性,值为 28。 - physics - 引用方法参数,值为 50。
由于条件(28 < 50)为 true,因此执行了 if 语句内的代码。
Swift mutating 方法
在 Swift 中,如果在类或结构体中声明了属性,则无法在方法中修改它们。例如,
struct Employee {
var salary = 0.0
...
func salaryIncrement() {
// Error Code
salary = salary * 1.5
}
这里,由于 struct 是值类型,如果我们尝试修改 salary 的值,我们会收到一条错误消息。
但是,如果我们要从方法内部修改值类型的属性,则需要在声明方法时使用 mutating 关键字。
示例:从方法修改值类型
struct Employee {
var salary = 0
// define mutating function
mutating func salaryIncrement(increase: Int) {
// modify salary property
salary = salary + increase
print("Increased Salary:",salary)
}
}
var employee1 = Employee()
employee1.salary = 20000
employee1.salaryIncrement(increase: 5000)
输出
Increased Salary: 25000
在上面的示例中,我们创建了一个名为 Employee 的结构体。请注意:
mutating func salaryIncrement(increase: Int) {
// modify salary property
salary = salary + increase
...
}
这里,mutating 方法允许我们在方法内部修改 salary 的值。