# Swift：实例方法和类型方法

1. 实例方法(减号开头)

- (instancetype)init;

2. 类型方法(加号开头)

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations

struct Point {
let x: Double
let y: Double

// 存储属性
static let zero = Point(x: 0, y: 0)

// 计算属性
static var ones: [Point] {
return [
Point(x: 1, y: 1),
Point(x: -1, y: 1),
Point(x: 1, y: -1),
Point(x: -1, y: -1)
]
}

// 类型方法
static func add(p1: Point, p2: Point) -> Point {
return Point(x: p1.x + p2.x, y: p1.y + p2.y)
}
}

let p = Point(x: 3, y: 4)

Point.zero
Point.ones
Point.add(Point(x: 1, y: 2), p2: Point(x: 2, y: 1))

class Square {
// 类型属性，用class关键字
class var PI: Double{
return 3.14
}
}

class Square {
class var PI: Double{
get { return 3.14 }
}
}

class Student {
class var phone: String?
}

class Student {
static var phone: String?
}

protocol MyProtocol {
static func foo() -> String
}

struct MyStruct: MyProtocol {
static func foo() -> String {
return "MyStruct"
}
}

class MyClass {
class func foo() -> String {
return "MyClass"
}
}