属性、方法以及继承
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import UIKit
|
||||
import CoreGraphics
|
||||
|
||||
|
||||
struct Circle {
|
||||
@@ -100,3 +101,70 @@ struct CircleA {
|
||||
var circleA = CircleA()
|
||||
circleA.radius = 10.5
|
||||
print(circleA.radius)
|
||||
|
||||
struct Shape {
|
||||
var width: Int
|
||||
var side: Int {
|
||||
willSet {
|
||||
print("willSetSide", newValue)
|
||||
}
|
||||
didSet {
|
||||
print("didSetSide", oldValue, side)
|
||||
}
|
||||
}
|
||||
var girth: Int {
|
||||
set {
|
||||
width = newValue / side
|
||||
print("setGirth", newValue)
|
||||
}
|
||||
get {
|
||||
print("getGirth")
|
||||
return width * side
|
||||
}
|
||||
}
|
||||
func show() {
|
||||
print("width=\(width), side=\(side), girth=\(girth)")
|
||||
}
|
||||
}
|
||||
|
||||
func test(_ num: inout Int) {
|
||||
num = 20
|
||||
}
|
||||
var s = Shape(width: 10, side: 4)
|
||||
test(&s.width)
|
||||
s.show()
|
||||
print("----------")
|
||||
test(&s.side)
|
||||
s.show()
|
||||
print("----------")
|
||||
test(&s.girth)
|
||||
s.show()
|
||||
|
||||
struct CarA {
|
||||
static var count: Int = 0
|
||||
init() {
|
||||
CarA.count += 1
|
||||
}
|
||||
}
|
||||
|
||||
let c1 = CarA()
|
||||
let c2 = CarA()
|
||||
let c3 = CarA()
|
||||
|
||||
print(CarA.count)
|
||||
|
||||
|
||||
// 单例模式
|
||||
public class FileManager {
|
||||
// 延迟初始化
|
||||
public static let shared = FileManager()
|
||||
// 禁止外部初始化
|
||||
private init() {}
|
||||
// 方法
|
||||
func run() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
FileManager.shared.run()
|
||||
|
||||
|
Reference in New Issue
Block a user