import Foundation
/*
方法
*/
//实例方法 一定需要依附于对象 class MyPoint {
var x: Double = 0.0
var y: Double = 0.0
//类中的内部方法 第一个参数默认没有外部参数名,从第二个以后开始,方法名作为外部参数名 既作为内部参数又做为外部参数
//实例方法
func set(_x: Double,_y: Double) {
x = _x
y = _y
}
//实例方法
func show() {
println("x:\(x) y\(y)")
}
}
func set(_x: Double,_y: Double) { } var p0 = MyPoint()
p0.set(, _y: )
p0.show()
set(, ) //结构体中的mutating方法
struct MyPoint_1 {
var x : Double =
var y : Double =
//结构体和枚举 值类型不可以直接修改其内的变量值,如果要修改,需要在方法前加关键字 mutating
mutating func set(x : Double,y : Double) {
self.x = x
self.y = y }
func show() {
println("x:\(self.x) y:\(self.y) ")
} }
//枚举中可以写方法,但枚举中不能有存储属性,可以有计算属性
enum LightSwitch {
case OFF,ON,HIGH
mutating func next() {
switch self {
case .OFF:
self = ON
case .ON:
self = OFF
case .HIGH:
self = OFF }
} }
var p1 = MyPoint_1()
p1.show() var light = LightSwitch.OFF
println(light.hashValue)
light.next()
println(light.hashValue) /*
类型方法 静态方法
通过类名+方法名来调用
与static静态方法相似,该方法为所有对象共用
*/
struct MyPoint_2 {
var p: Int =
static var sp: Int =
func getvalue() {
println("p:\(p) sp:\(MyPoint_2.sp)")
}
//静态方法不能够访问非静态变量
static func static_getvalue() {
// println("p:\(p) sp:\(MyPoint_2.sp)")
println("sp:\(MyPoint_2.sp)") } }
struct MyPoint_3 {
var p: Int =
static var sp: Int =
func getvalue() {
println("p:\(p) sp:\(MyPoint_3.sp)")
}
//静态方法不能够访问非静态变量
static func static_getvalue() {
// println("p:\(p) sp:\(MyPoint_3.sp)")
println("sp:\(MyPoint_3.sp)") } }
var m2 = MyPoint_2()
m2.getvalue()
MyPoint_2.static_getvalue() var m3 = MyPoint_3()
m3.getvalue()
MyPoint_3.static_getvalue() /*
subscripts 下标 访问对象中数据的快捷方式
实例[索引值]
*/
let array = [,,,,,,]
println(array[]) //实例对象[索引] struct student {
var name : String = ""
var math : Int
var english : Int
var chinese : Int
func scoreOf(course : String) -> Int? {
switch course {
case "math":
return math
case "chinese":
return chinese
case "english":
return english
default:
return nil
}
}
//制作下标方法
/*
subscript (course : String) -> Int? {
switch course {
case "math":
return math
case "chinese":
return chinese
case "english":
return english
default:
return nil
}
}
*/
//或者这么写
subscript (course : String) -> Int? {
get{
switch course {
case "math":
return math
case "chinese":
return chinese
case "english":
return english
default:
return nil
}
}
set{
switch course {
case "math":
math = newValue!
case "chinese":
chinese = newValue!
case "english":
english = newValue!
default:
println("set error")
}
}
} }
var li = student(name: "lisi", math: , english: , chinese: )
println(li.scoreOf("math"))
println(li["math"])//下标访问
li["math"] = //下标赋值
println(li["math"])//下标访问 //下标多索引
struct Mul {
subscript (a: Int,b: Int) -> Int {
return a*b
}
}
var mul = Mul()
println(mul[,])
 

初学swift笔记 方法(九)的更多相关文章

  1. swift 笔记 (十九) —— 协议

    协议(Protocols) 协议仅是用定义某些任务或者是功能必须的方法和属性. 类似于java里的interface的作用.但协议并不会实现详细的功能. 我猜这个名字源于OO中提到的"契约& ...

  2. 初学swift笔记 结构体(八)

    import Foundation /* 和类很相似 结构体 -> 封装 属性.方法 结构体是值类型 */ //定义一个LSQ类型的结构体 关键字struct struct LSQ { var ...

  3. 初学swift笔记 继承(十)

    import Foundation /* 继承 class childClass: FatherClass{ } 优点 代码重用 缺点 增加程序的藕合度 父类的改变会影响子类 只能单继承 */ cla ...

  4. 初学swift笔记 枚举(七)

    import Foundation /* 枚举 语法结构 enum 类型 { case 值 } */ //定义一个枚举 写法1 enum LiuShiQun { case Name case Age ...

  5. 初学swift笔记运算符(二)

    swift 中的运算符 import Foundation //字符类型 var char: Character = "a" char="b" println( ...

  6. 初学swift笔记变量的定义(一)

    swift变量的定义 1 import Foundation /* 变量的定义 变量的类型是可以不用写的 var a=10 常量的定义 let修饰 */ print(a) let b= print(b ...

  7. 初学swift笔记 函数(六)

    import Foundation /* func 函数名 (参数名:参数类型) { } func 函数名 (参数名:参数类型) ->Void{ } func 函数名 (参数名:参数类型) -& ...

  8. 初学swift笔记 流程控制(五)

    import Foundation ; i<=; i++ { println(i) } let str1="adl;fjasdfl;ouewrouqwperuadf" for ...

  9. 初学swift笔记字典、数组(四)

    import Foundation //字典 元素顺序是无序的 //1.字典元素是键值对 (key:value) //key 一定是可哈希的 string\int\bool var dic1=[&qu ...

随机推荐

  1. ssh整合web.xml过滤器和监听器的配置 .

    延迟加载过滤器 Hibernate 允许对关联对象.属性进行延迟加载,但是必须保证延迟加载的操作限于同一个 Hibernate Session 范围之内进行.如果 Service 层返回一个启用了延迟 ...

  2. [TYVJ] P1049 最长不下降子序列

    最长不下降子序列 描述 Description 求最长不下降子序列的长度   输入格式 InputFormat 第一行为n,表示n个数第二行n个数   输出格式 OutputFormat 最长不下降子 ...

  3. 使用SC命令时注意事项

    使用SC命令时注意事项[转] Windows 2003 Server存在一个sc命令,(好像Windows 2000/XP/NT都有这个.)该命令可以手工创建Windows服务(NT Service) ...

  4. C# 编写服务 Windows service

    1.编写服务教程 http://jingyan.baidu.com/article/ea24bc395e16f8da62b331e7.html 这里不多说了. 给大家一个连接,上面有详细的教程,下面说 ...

  5. (?:pattern) (?=pattern) (?!pattern)

    (pattern) 匹配 pattern 并获取这一匹配.所获取的匹配可以从产生的 Matches 集合得到,在VBScript 中使用 SubMatches 集合,在JScript 中则使用 $0- ...

  6. (转)ZOJ 3687 The Review Plan I(禁为排列)

    The Review Plan I Time Limit: 5 Seconds      Memory Limit: 65536 KB Michael takes the Discrete Mathe ...

  7. hdu 4750 Count The Pairs(并查集+二分)

    Problem Description With the 60th anniversary celebration of Nanjing University of Science and Techn ...

  8. 模块工具类--utils

    File: js\utils.js/** * 模块工具类,用来初始化各模块视图.自定绑定事件以及其他辅助功能等 * @class Utils */Utils = (function() { var i ...

  9. 把Storyboard减轻的方法

    把Storyboard减轻的方法 by 伍雪颖 UIViewController *secondStoryboard = [[UIStoryboard storyboardWithName:@&quo ...

  10. idea maven jetty插件热部署

    maven tomcat插件好像无法进行热部署,jetty可以如下配置实现热部署,但是idea无法进行自动编译,所以需要如下快捷键 Ctrl+Shift+F9,编译 Ctrl+F9,生成项目 < ...