详解Swift编程中的方法与属性的概念.doc

上传人:scccc 文档编号:14787975 上传时间:2022-02-19 格式:DOC 页数:10 大小:55.50KB
返回 下载 相关 举报
详解Swift编程中的方法与属性的概念.doc_第1页
第1页 / 共10页
详解Swift编程中的方法与属性的概念.doc_第2页
第2页 / 共10页
详解Swift编程中的方法与属性的概念.doc_第3页
第3页 / 共10页
亲,该文档总共10页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《详解Swift编程中的方法与属性的概念.doc》由会员分享,可在线阅读,更多相关《详解Swift编程中的方法与属性的概念.doc(10页珍藏版)》请在三一文库上搜索。

1、详解 Swift 编程中的方法与属性的概念方法在 Swift 中特定类型的相关联功能被称为方法。在 Objective C 中类是用来定义方法,其 中作为 Swift 语言为用户提供了灵活性,类,结构和枚举中可以定义使用方法。实例方法在 Swift 语言,类,结构和枚举实例通过实例方法访问。1. 实例方法提供的功能2. 访问和修改实例属性3. 函数关联实例的需要实例方法可以写在花括号 内。它隐含的访问方法和类实例的属性。当该类型指定具体实 例它调用获得访问该特定实例。语法复制代码 代码如下 :func funcname(Parameters)- returntype Statement1Sta

2、tement2-Statement N return parameters示例复制代码 代码如下 :class calculations let a:Intlet b:Intlet res:Int init(a:Int, b:Int)self.a = aself.b = bres = a + bfunc tot(c:Int)-Intreturn res - c func result()println(Result is: (tot(20)println(Result is: (tot(50)let pri = calculations(a:600, b:300)pri.result()当我们

3、使用 playground 运行上面的程序,得到以下结果Result is: 880Result is: 850Calculations 类定义了两个实例方法:init() 被定义为两个数 a 和 b 相加,并将其结果存储在 restot() 用于通过从 “ res” 值减去 c最后,调用打印的计算 a 和 b 的值方法 . 实例方法以 . 语法访问 局部和外部参数名称Swift 函数描述了局部和全局变量声明。同样, Swift 方法的命名规则也类似 Objective C 。 但是局部和全局参数名称声明的特性对于函数和方法不同。 swift 第一个参数是由介词名称 with, for 和 b

4、y 访问命名规则。Swift 提供声明作为局数参数名称,其它参数名称为全局参数名,第一参数是方法名称。在 这里,“nol”方法作为局部参数名来声明。no2用于全局声明,并通过该程序访问。复制代码 代码如下 :class division var count:Int=0func incrementBy(no1:Int, no2:Int)count = no1 / no2println(count)let counter = division()counter.incrementBy(1800, no2:3)counter.incrementBy(1600, no2:5)counter.incre

5、mentBy(11000, no2:3)当我们使用 playground 运行上面的程序,得到以下结果6003203666外部参数名称使用 # 和 _ 符号尽管 Swift 方法提供第一个参数名称作为局部声明,用户必须提供以修改参数名称从局部 到全局声明。这可以通过 符号前缀使用第一参数名来完成。通过这样做,第一参数可以 作为全局在整个模块访问。当用户需要使用外部名称访问在后面的参数名中,方法的名字使用“_”符号覆盖。复制代码 代码如下 :class multiplication var count:Int=0func incrementBy(#no1:Int, no2:Int)count =

6、 no1 * no2println(count)let counter = 当我们使用 playground 运行上面的程序,得到以下结果240050045000在方法中的 Self 属性方法有一个隐式属性被称为“self”,所有定义的类型实例所都有。“ self”属性被用于表示当前的实例定义的方法。复制代码 代码如下 :class calculations let a:Intlet b:Intlet res:Intinit(a:Int, b:Int)self.a = aself.b = bres = a + b println(Inside Self Block: (res)func tot

7、(c:Int)-Intreturn res - cfunc result()println(Result is: (tot(20)println(Result is: (tot(50)let pri = calculations(a:600, b:300)let sum =sum.result()当我们使用 playground 运行上面的程序,得到以下结果Inside Self Block: 900Inside Self Block: 1500Result is: 880Result is: 850Result is: 1480Result is: 1450修改的实例方法值类型在 Swift

8、 语言结构和枚举和值类型不能由它的实例方法来改变。然而, swift 语言通过 “变异”行为提供了灵活修改值类型。 突变将使得在实例方法中的任何变化, 将方法执行之后变 化返回到原来的形式。此外,由 “selft ” 属性的新实例其隐式函数创建,执行之后将取代 现有的方法复制代码 代码如下 :struct area var length =1var breadth =1func area()-Intreturn length * breadthmutating func scaleBy(res:Int)length *= res breadth *= resprintln(length) pr

9、intln(breadth)var val = area(length:3, breadth:5) val.scaleBy(3) val.scaleBy(30) val.scaleBy(300)当我们使用 playground 运行上面的程序,得到以下结果Self 属性的不同诱变方法 突变方法结合 “ self” 属性分配给新实例所定义的方法。复制代码 代码如下 :struct area var length =1var breadth =1func area()-Intreturn length * breadthmutating func scaleBy(res:Int)self.leng

10、th *= resself.breadth *= resprintln(length) println(breadth)var val = area(length:3, breadth:5) val.scaleBy(13)当我们使用 playground 运行上面的程序,得到以下结果3965类型方法当方法的特定实例调用,它调用一个实例方法并且当方法调用特定类型的方法的一个被定义为类型方法“。类型方法“类”是由“func”关键字和结构定义,和枚举型方法使用“func”关键字之前的static ”关键字定义。类型方法调用,是通过访问 . 而不是调用特定实例的方法,例子和语法如下:复制代码 代码如下

11、 :classMathclass func abs(number:Int)-Intif number Intif number oldValue println(Newly Added Counter (counter - oldValue)letNewCounter=Samplepgm()NewCounter.counter =100NewCounter.counter =800当我们使用 playground 运行上面的程序,我们得到以下结果Total Counter is: 100Newly Added Counter 100Total Counter is: 800Newly Adde

12、d Counter 700局部和全局变量对于计算和观察属性局部和全局变量的声明。类型属性属性定义类型定义部分有大括号 ,并且变量的范围也被前面所定义。要定义值类型使用 “static ” 关键字以及类的类型使用 “ class” 关键字。语法复制代码 代码如下 :structStructnamestaticvar storedTypeProperty = staticvar computedTypeProperty:Int/ return an Int value hereenumEnumnamestaticvar storedTypeProperty = staticvar computed

13、TypeProperty:Int/ return an Int value hereclassClassnameclassvar computedTypeProperty:Int/ return an Int value here查询和设置属性类似于实例属性类型属性查询和设置,只是使用“ .” 语法,而不用指向该实例的类型。复制代码 代码如下 :structStudMarksstaticlet markCount =97staticvar totalCount =0varInternalMarks:Int=0 didSet ifInternalMarksStudMarks.markCount

14、InternalMarks=StudMarks.markCount ifInternalMarksStudMarks.totalCount StudMarks.totalCount =InternalMarksvar stud1Mark1 =StudMarks()var stud1Mark2 =StudMarks()stud1Mark1.InternalMarks=98println(stud1Mark1.InternalMarks)stud1Mark2.InternalMarks=87println(stud1Mark2.InternalMarks)当我们使用 playground 运行上面的程序,我们得到以下结果

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1