Swift_方法


点击查看源码

  1. ///方法
  2. class Methods: NSObject {
  3. func test() {
  4. // self.testInstanceMethods() //实例方法
  5. self.testTypeMethods() //类型方法
  6. }
  7. //实例方法
  8. func testInstanceMethods() {
  9. self.testLocalAndExternalParameterNames()
  10. self.testSelfProperty()
  11. self.testModifyingValueTypesFromWithinInstanceMethods()
  12. self.testAssigningToSelfWithinMutatingMethod()
  13. }
  14. //内部和外部属性
  15. func testLocalAndExternalParameterNames() {
  16. class Counter {
  17. var count: Int = 0
  18. func incrementBy(_ amount: Int, numberOfTimes: Int) {
  19. count += amount * numberOfTimes
  20. }
  21. }
  22. let counter = Counter()
  23. counter.incrementBy(5, numberOfTimes: 3)
  24. print("\(counter.count)")
  25. /* print
  26. 15
  27. */
  28. }
  29. //self属性
  30. func testSelfProperty() {
  31. //self属性
  32. struct Point {
  33. var x = 0.0, y = 0.0
  34. func isToTheRightOfX(_ x: Double) -> Bool {
  35. //这里有内部和外部属性
  36. return self.x > x
  37. }
  38. }
  39. let somePoint = Point(x: 4.0, y: 5.0)
  40. print("\(somePoint.isToTheRightOfX(1.0))")
  41. /*
  42. true
  43. */
  44. }
  45. //在实例方法中修改值类型
  46. func testModifyingValueTypesFromWithinInstanceMethods() {
  47. //因为结构体是值对象,其内部方法无法修改外部值,为了让结构体支持修改结构体内的属性。
  48. //方法体前加mutating,让结构体的实例方法可以修改结构体中的值
  49. struct Point {
  50. var x = 0.0, y = 0.0
  51. mutating func moveByX(_ deltaX: Double, y deltaY: Double) {
  52. x += deltaX
  53. y += deltaY
  54. }
  55. }
  56. var somePoint = Point(x: 1.0, y: 1.0)
  57. somePoint.moveByX(2.0, y: 3.0)
  58. print("(\(somePoint.x), \(somePoint.y))")
  59. let fixedPoint = Point(x: 3.0, y: 3.0)
  60. print(fixedPoint)
  61. // 结构体是值对象,使用let常量后,无法修改内部值
  62. // fixedPoint.moveByX(2.0, y: 3.0) // 抛错
  63. /* print
  64. (3.0, 4.0)
  65. (Point #1)(x: 3.0, y: 3.0)
  66. */
  67. }
  68. //自我变异
  69. func testAssigningToSelfWithinMutatingMethod() {
  70. //mutating还可以修改当前结构体和当前枚举
  71. //结构体测试
  72. struct Point {
  73. var x = 0.0, y = 0.0
  74. mutating func moveByX(_ deltaX: Double, y deltaY: Double) {
  75. self = Point(x: x + deltaX, y: y + deltaY)
  76. }
  77. }
  78. var point = Point()
  79. point.moveByX(1.0, y: 1.0)
  80. print(point)
  81. //枚举测试
  82. enum TriStateSwitch {
  83. case off, low, high
  84. mutating func next() {
  85. switch self {
  86. case TriStateSwitch.off:
  87. self = TriStateSwitch.low
  88. print(self)
  89. case TriStateSwitch.low:
  90. self = TriStateSwitch.high
  91. print(self)
  92. case TriStateSwitch.high:
  93. self = TriStateSwitch.off
  94. print(self)
  95. }
  96. }
  97. }
  98. var ovenLight = TriStateSwitch.low
  99. ovenLight.next()
  100. ovenLight.next()
  101. /*
  102. (Point #1)(x: 1.0, y: 1.0)
  103. high
  104. off
  105. */
  106. }
  107. //类型方法
  108. func testTypeMethods() {
  109. //类
  110. class SomeClass {
  111. class func someTypeMethod() {
  112. // type method implementation goes here
  113. }
  114. }
  115. SomeClass.someTypeMethod()
  116. // 结构体
  117. struct LevelTracker {
  118. // static修改属性,方法体要修改static属性,方法前要使用static
  119. static var highestUnlockedLevel = 1
  120. static func levelIsUnlocked(_ level: Int) -> Bool {
  121. return level <= highestUnlockedLevel
  122. }
  123. }
  124. print("\(LevelTracker.levelIsUnlocked(2))")
  125. /*
  126. false
  127. */
  128. }
  129. }

Swift_方法的更多相关文章

  1. javaSE27天复习总结

    JAVA学习总结    2 第一天    2 1:计算机概述(了解)    2 (1)计算机    2 (2)计算机硬件    2 (3)计算机软件    2 (4)软件开发(理解)    2 (5) ...

  2. Swift_协议

    Swift_协议 点击查看源码 //协议 @objc protocol SomeProtocol:class { //class代表只用类才能实现这个协议 func test() //@objc:OC ...

  3. Swift_销毁

    Swift_销毁 点击查看源码 销毁 func test() { class SomeClass { //类销毁时 通知此方法 deinit { print("销毁") } } v ...

  4. Swift_初始化

    #Swift_初始化 点击查看源码 初始化结构体 //初始化结构体 func testInitStruct() { //结构体 类中默认方法 struct Size { //宽 var width = ...

  5. Swift_继承

    Swift_继承 点击查看源码 func testInheritance() { //基类 class Base { var count = 0.0 var description: String { ...

  6. mapreduce多文件输出的两方法

    mapreduce多文件输出的两方法   package duogemap;   import java.io.IOException;   import org.apache.hadoop.conf ...

  7. 【.net 深呼吸】细说CodeDom(6):方法参数

    本文老周就给大伙伴们介绍一下方法参数代码的生成. 在开始之前,先补充一下上一篇烂文的内容.在上一篇文章中,老周检讨了 MemberAttributes 枚举的用法,老周此前误以为该枚举不能进行按位操作 ...

  8. IE6、7下html标签间存在空白符,导致渲染后占用多余空白位置的原因及解决方法

    直接上图:原因:该div包含的内容是靠后台进行print操作,输出的.如果没有输出任何内容,浏览器会默认给该空白区域添加空白符.在IE6.7下,浏览器解析渲染时,会认为空白符也是占位置的,默认其具有字 ...

  9. 多线程爬坑之路-Thread和Runable源码解析之基本方法的运用实例

    前面的文章:多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类) 多线程爬坑之路-Thread和Runable源码解析 前面 ...

随机推荐

  1. 洛谷P3952 时间复杂度(模拟)

    题意 题目链接 Sol 咕了一年的题解..就是个模拟吧 考场上写的递归也是醉了... 感觉一年自己进步了不少啊..面向数据编程的能力提高了不少 #include<bits/stdc++.h> ...

  2. 微信小程序request请求之GET跟POST的区别

    1.GET 例子: wx.request({ url: 'test.php', //仅为示例,并非真实的接口地址 data: { x: '' , y: '' }, header: { 'content ...

  3. 关于github改名问题

    不喜欢github显示的目录名字于是百度了下,更改过程,记录下来,方便日后查看! 首页右上角点击出来菜单,找到Settings按钮点击 左侧找到Account账号菜单点击 找到change usern ...

  4. Mysql与web之间的数据、查询等个问题

    Mysql与web之间的数据.查询等个问题 在自己写的一个jsp主页连接数据库出现的各种问题,写记下来与大家分享,共勉.最后附jdbc代码. ---DanlV Error 1---错误代码: java ...

  5. Hibernate (ORM)

    1 框架体系结构 2 hibernate入门 2.1 ORM框架 Hibernate是一个数据持久化层的ORM框架. Object:对象,java对象,此处特指JavaBean Relational: ...

  6. hdu 1102 Constructing Roads (Prim算法)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...

  7. SQL Server ->> Online Index Rebuilding(联机索引重建)

    SQL Server的Enterprise Edition是支持联机索引重建的.那么联机索引重建是怎么工作的以及对我们的查询有什么影响呢? 既然是联机,SQL Server保持了现有索引对于用户的可用 ...

  8. Data Flow ->> Multiple Excel Sheet Loaded Into One Table

    同个Excel文件中多个Sheet中的数据导入到单张表中,参考了文章:http://www.cnblogs.com/biwork/p/3478778.html 思路: 1) ForEach Loop组 ...

  9. U盘中毒了?教你如何删除System Volume Information这个顽固文件夹

    不得不说cmd命令很好用呢.最近我的U盘中毒了,格式化都删除不了System Volume Information这个顽固的文件夹,真心伤不起哇!还好现在解决了问题.看来以后得好好对待U盘,不能乱用了 ...

  10. 在win7 windows 上编译 beego 上传到 linux 去执行

    在beego的项目目录下,执行: GOOS=linux GOARCH=amd64 go build So easy!但是却搞了好久! 参考连接:http://blog.csdn.net/changji ...