Swift 方法

2019-10-28  本文已影响0人  霸_霸霸

1、不带返回值的方法

func methodWithoutReturnValue(a : Int) {
    print(a)
}

2、带返回值的方法

func methodWithReturnValue(a : Int) -> Int {
    return a + 10
}

3、带参数标签的方法,只显示参数标签,不显示参数名

    func writeLetterTo(to aName : String, and bName : String) {
        
        //code
    }

//调用
    self.writeLetterTo(to: "Jack", and: "Peyton")

4、带有默认参数值的方法

    func printTwoValue(a : Int, b : Int = 10)  {
        //code
    }

    //有2种调用方法
    //1. 只给a赋值,b使用默认的10
    printTwoValue(a: 2)
    //2. 给a,b都赋值,不使用b的默认值
    printTwoValue(a: 2, b: 3)

5、带有可变参数的方法

    func printNames(_ names : String...)  {
        for index in names {
            print(index)
        }
    }

    //调用
    self.printNames("Jack", "Lucy", "Michael")

6、通过inout关键字在方法内部修改外面变量(不是对象)的值
我们知道外部的变量,在作为参数时,是拷贝一份全新的值,赋给方法的参数,所以,我们在方法中对参数进行操作的时候,是不会改变外部变量的值的;Swift提供了inout关键字,允许我们在方法内部修改外部变量的值,例子:

var a = 10
var b = 20
func exchangeOutValues(a : inout Int, b : inout Int) {
    //交换啊a,b的值
    let c = a
    a = b
    b = c
}

//调用
exchangeOutValues(a: &a, b: &b)

a 和 b 的值会互换

7、方法也是对象,它的类型由参数和返回值决定

    //定义一个method
    var method : (Int, Int) -> Int

    //一个方法的实现
    func addTwoIntsValue(a : Int, b : Int) -> Int {
        return a + b    
    }
    //给method赋值
    method = addTwoIntsValue(a:b:)
    //调用
    print(method(10, 20))

8、方法作为方法的参数

    //声明一个方法doSomething,其参数为:类型为(Int) -> Int的方法
    func doSomething(method : (Int) -> Int) {
        print(method(10))
    }
    //调用doSomething方法
    self.doSomething { (a : Int) -> Int in
        return a + 1
    }

结果为 11
上面调用doSomething的时候,方法作为参数的形式很像是闭包

9、方法作为方法的返回值

    //doSomething方法返回一个类型为 (Int)->Int 的方法
    func doSomething() -> (Int) -> Int {
        return self.returnMethod(a:)
    }
    
    //这个方法将作为doSomething方法的返回值
    func returnMethod(a : Int) -> Int {
        return a + 1
    }

    //调用doSomething方法,用一个新的方法接收doSomething方法的返回值
    let method = self.doSomething()

    //调用最终的方法
    print(method(100))

结果为101

类方法和实例方法

1、实例方法

1.1 实例方法的声明

    func 方法名(参数标签 参数名 : 参数类型) -> 返回值类型 {
        方法体
    }

上面的例子都是实例方法,不再赘述

1.2 实例方法的继承

声明两个类ClassA,ClassB继承自ClassA

class ClassA : NSObject {
    override init() {
        super.init()
    }
            
    func instanceMethod()  {
        print("instant method A")
    }
}

class ClassB : ClassA {
    override init() {
        super.init()   
    }
    //override表示重写父类的方法
    override func instanceMethod() {
        print("instant method B")
    }
}

调用实例方法

    let a = ClassA.init()
    let b = ClassB.init()
        
    a.instanceMethod()
    b.instanceMethod()

结果显而易见
instant method A
instant method B

2、类方法

2.1、如何声明类方法

一般情况下,我们是通过static关键字来声明类方法,但是如果一个类方法需要在子类中重写,那就必须使用class关键字

//不可被子类重写
static func classMethod1() {
    print("1")
}
//可被子类重写
class func classMethod2() {
    print("2")
}

2.2、类方法如何继承

class ClassA : NSObject {
    static func classMethod1() {
        print("1")
    }
    class func classMethod2() {
         print("2")
    }
}

class ClassB : ClassA {
    override class func classMethod2() {
        print("4")
    }
}

classMethod2是通过class关键字声明的,所以可以在ClassB中进行重写;如果你要对classMethod1进行重写,会报错
调用

ClassA.classMethod1()
ClassA.classMethod2()
ClassB.classMethod1()
ClassB.classMethod2()

结果
1
2
1
4

上一篇 下一篇

猜你喜欢

热点阅读