• 初学swift笔记 方法(九)


      1 import Foundation
      2 /*
      3 方法
      4 */
      5 //实例方法 一定需要依附于对象
      6 
      7 class MyPoint {
      8     var x: Double = 0.0
      9     var y: Double = 0.0
     10     //类中的内部方法 第一个参数默认没有外部参数名,从第二个以后开始,方法名作为外部参数名 既作为内部参数又做为外部参数
     11     //实例方法
     12     func set(_x: Double,_y: Double) {
     13         x = _x
     14         y = _y
     15     }
     16     //实例方法
     17     func show() {
     18         println("x:(x) y(y)")
     19     }
     20 }
     21 func set(_x: Double,_y: Double) {
     22     
     23 }
     24 
     25 var p0 = MyPoint()
     26 p0.set(10, _y: 7)
     27 p0.show()
     28 set(10, 10)
     29 
     30 
     31 //结构体中的mutating方法
     32 struct MyPoint_1 {
     33     var x : Double = 0
     34     var y : Double = 0
     35     //结构体和枚举 值类型不可以直接修改其内的变量值,如果要修改,需要在方法前加关键字 mutating
     36     mutating func set(x : Double,y : Double) {
     37         self.x = x
     38         self.y = y
     39         
     40     }
     41     func show() {
     42         println("x:(self.x) y:(self.y) ")
     43     }
     44     
     45 }
     46 //枚举中可以写方法,但枚举中不能有存储属性,可以有计算属性
     47 enum LightSwitch {
     48     case OFF,ON,HIGH
     49     mutating func next() {
     50         switch self {
     51         case .OFF:
     52             self = ON
     53         case .ON:
     54             self = OFF
     55         case .HIGH:
     56             self = OFF
     57             
     58         }
     59     }
     60     
     61 }
     62 var p1 = MyPoint_1()
     63 p1.show()
     64 
     65 var light = LightSwitch.OFF
     66 println(light.hashValue)
     67 light.next()
     68 println(light.hashValue)
     69 
     70 /*
     71 类型方法 静态方法
     72 通过类名+方法名来调用
     73 与static静态方法相似,该方法为所有对象共用
     74 */
     75 struct MyPoint_2 {
     76     var p: Int = 0
     77     static var sp: Int = 0
     78     func getvalue() {
     79         println("p:(p) sp:(MyPoint_2.sp)")
     80     }
     81     //静态方法不能够访问非静态变量
     82     static func static_getvalue() {
     83         // println("p:(p) sp:(MyPoint_2.sp)")
     84         println("sp:(MyPoint_2.sp)")
     85 
     86     }
     87     
     88 }
     89 struct MyPoint_3 {
     90     var p: Int = 0
     91     static var sp: Int = 0
     92     func getvalue() {
     93         println("p:(p) sp:(MyPoint_3.sp)")
     94     }
     95     //静态方法不能够访问非静态变量
     96     static func static_getvalue() {
     97         // println("p:(p) sp:(MyPoint_3.sp)")
     98         println("sp:(MyPoint_3.sp)")
     99         
    100     }
    101     
    102 }
    103 var m2 = MyPoint_2()
    104 m2.getvalue()
    105 MyPoint_2.static_getvalue()
    106 
    107 var m3 = MyPoint_3()
    108 m3.getvalue()
    109 MyPoint_3.static_getvalue()
    110 
    111 /*
    112 subscripts 下标 访问对象中数据的快捷方式
    113 实例[索引值]
    114 */
    115 let array = [1,2,3,4,5,6,7]
    116 println(array[0]) //实例对象[索引]
    117 
    118 struct student {
    119     var name : String = ""
    120     var math : Int
    121     var english : Int
    122     var chinese : Int
    123     func scoreOf(course : String) -> Int? {
    124         switch course {
    125             case "math":
    126             return math
    127             case "chinese":
    128             return chinese
    129             case "english":
    130             return english
    131         default:
    132             return nil
    133         }
    134     }
    135     //制作下标方法
    136     /*
    137     subscript (course : String) -> Int? {
    138         switch course {
    139         case "math":
    140             return math
    141         case "chinese":
    142             return chinese
    143         case "english":
    144             return english
    145         default:
    146             return nil
    147         }
    148     }
    149     */
    150     //或者这么写
    151     subscript (course : String) -> Int? {
    152     get{
    153         switch course {
    154         case "math":
    155             return math
    156         case "chinese":
    157             return chinese
    158         case "english":
    159             return english
    160         default:
    161             return nil
    162         }
    163     }
    164         set{
    165             switch course {
    166             case "math":
    167                  math = newValue!
    168             case "chinese":
    169                  chinese = newValue!
    170             case "english":
    171                  english = newValue!
    172             default:
    173                  println("set error")
    174             }
    175         }
    176     }
    177 
    178 }
    179 var li = student(name: "lisi", math: 90, english: 80, chinese: 98)
    180 println(li.scoreOf("math"))
    181 println(li["math"])//下标访问
    182 li["math"] = 100 //下标赋值
    183 println(li["math"])//下标访问
    184 
    185 //下标多索引
    186 struct Mul {
    187     subscript (a: Int,b: Int) -> Int {
    188         return a*b
    189     }
    190 }
    191 var mul = Mul()
    192 println(mul[3,6])
     
  • 相关阅读:
    Eclipse配置Maven详细教程
    Spring MVC 搭建web项目示例
    C# Action 和Func
    C# params 用法简介
    WPF绘图性能问题
    C# EventWaitHandle用法
    C#5.0 异步编程async/await用法
    通过Struts2Web应用框架深入理解MVC
    Java过滤器—Filter用法简介
    WPF内嵌CEF控件,与JS交互
  • 原文地址:https://www.cnblogs.com/lsqandzy/p/4912808.html
Copyright © 2020-2023  润新知