swift - 类型方法(即静态变量)
2016-04-27 17:12
232 查看
在swift中静态变量被称为类型方法
类:前加class。
杖举:前加static
如果这时在方法中使用self,那么self就是指类型本身,而不是指一个具体的类型实例。
struct MyStruct {
static
var x:Int =
100
static
var y:Int =
100
//类型方法
static
func method(x:Int){
var y =
300 300
print("x =
\(x)") "x = 1000\n"
print("y =
\(y)") "y = 300\n"
print("self.x =
\(self.x)") "self.x
= 100\n"
print("self.y =
\(self.y)") "self.y
= 100\n"
}
}
MyStruct.method(1000)
MyStruct.x =
5
类:前加class。
杖举:前加static
如果这时在方法中使用self,那么self就是指类型本身,而不是指一个具体的类型实例。
struct MyStruct {
static
var x:Int =
100
static
var y:Int =
100
//类型方法
static
func method(x:Int){
var y =
300 300
print("x =
\(x)") "x = 1000\n"
print("y =
\(y)") "y = 300\n"
print("self.x =
\(self.x)") "self.x
= 100\n"
print("self.y =
\(self.y)") "self.y
= 100\n"
}
}
MyStruct.method(1000)
MyStruct.x =
5
相关文章推荐
- Swift UITableViewCell中的按钮点击事件
- swift - mutating(杖举或结构体,通过方法来改变自身的值)
- swift - 类型中的self
- swift 中的“..<”,“ ...“以及“_”
- swift2.0 学习笔记four
- UIImageView圆角设置swift
- Swift编译时报错:Command failed due to signal: Segmentation fault: 11
- Swift2.0 代理的使用
- Swift学习 (长期)
- Swift3.0转场动画的使用
- swift - “_”
- 你TM能不能告诉我xcode7.0的是swift什么版本
- swift - 不成文规定
- swift - 方法和全局函数
- Swift
- Swift2.0 泛型-减少重用代码的编写
- 编写Swift代码的其他工具
- swift面向对象之多态与继承
- swift面向对象之多态与继承
- 【Swift学习】Swift编程之旅---类和结构体(十三)