//
// ViewController.swift
// static_class
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// static 可以在类、结构体、或者枚举中使用,而 class 只能在类中使用。
// static 可以修饰存储属性,static 修饰的存储属性称为静态变量(常量)。而 class 不能修饰存储属性。
// static 修饰的不能被重写。而 class 修饰的可以被重写。
print("\(TestClass.doubleNumber2())") //❤️ 调用类方法
let test = TestClass()
test.usualFuction() // ❤️调用一般方法
}
// 1.static 关键字可以在结构体中使用
struct TestStruct {
static var number = 10
static func doubleNumber() -> Int {
return number * 2
}
}
// 2.class 关键字只可以在类中使用
class TestClass {
static var number = 10
static func doubleNumber() -> Int {
return number * 2
}
// class修饰计算属性、不可以修饰存储属性
class var number2: Int {
return 10
}
// 3.class修饰的类方法,可以直接用TestClass调用,不需要实例化。
class func doubleNumber2() -> Int {
return number * 2
}
// 4.一般方法
func usualFuction() {
print("一般方法的调用")
}
}
}