swift入門第三季(枚舉,類,面向對象, 協議)

枚舉

枚舉形式:

enum 枚舉名{
    //使用case關鍵字列出所有枚舉值
}

關聯值:

enum plant{
    case Eerth(weight:Double,name:String)
}

class User {
    //存儲屬性
    var first: String = "";
    var last: String = "";
    //計算屬性
    var fullName: String {
        get{
            return first + "-" + last;
        }
        set(newValue){
            var names = newValue.components(separatedBy: "-");
            self.first = names[0];
            self.last = names[1];
        }
    }
    //構造器
    init(first:String, last:String) {
        self.first = first;
        self.last = last;
    }
}
let s = User(first: "極客", last: "Hello");
print(s.fullName)
s.fullName = "測試-測試2"
print(s.fullName)

屬性觀察器

class Person {
    var age: Int = 0 {
        willSet {
            if newValue < 0 || newValue > 200 {
                print("年齡\(newValue)設置不符合要求,請重新設置!")
            }else {
                print("年齡設置符合要求,設置成功")
            }
        }
        didSet {
            print("年齡設置完成,被修改的年齡爲:\(oldValue)")
        }
    }
}
var p = Person();
p.age = 220;

兩種類中的方法,實例方法和類型方法

class SomeClass {
    func test() {
        print("==test 方法 ==")
    }
    class func bar(Msg msg:String) {
        print("==bar類型方法==,傳入的參數是:\(msg)")
    }
}
var sc = SomeClass()
var f1:()->() = sc.test;//通過對象進行調用
var f2:(String)->Void = SomeClass.bar//通過類進行調用
f1()
f2("極客")

方法有多個參數的,除第一個外,其他都要帶參數名調用。
如果不想要帶參數名,可以使用_修飾參數名

可變方法,mutating

struct JKRect {
    var x:Int
    var y:Int
    var width:Int
    var height:Int
    mutating func moveByX(x:Int,y:Int) {
        self.x += x
        self.y += y
    }
}
var rect = JKRect(x: 20, y: 12, width:200, height: 300)
rect.moveByX(x: 100, y: 90)
print("rect矩形的左上角的x座標爲:\(rect.x),y座標爲:\(rect.y)")

下標,用subscript修飾

struct JKRect {
    var x:Int
    var y:Int
    var width:Int
    var height:Int
    subscript (index: Int) -> Int {
        get{
            switch(index) {
                case 0:
                    return self.x
                case 1:
                    return self.y
                case 2:
                    return self.width
                case 3:
                    return self.height
                default:
                    print("不支持該索引值")
                    return 0
            }
        }
        set{
            switch(index) {
                case 0:
                    self.x = newValue
                case 1:
                    self.y = newValue
                case 2:
                    self.width = newValue
                case 3:
                    self.height = newValue
                default:
                    print("不支持該索引值")
            }
        }
    }
}
var rect2 = JKRect(x: 20, y: 12, width:200, height: 300)
print("rect矩形的左上角的x座標爲:\(rect2.x),y座標爲:\(rect2.y)")

可選鏈

class Customer{
    var name=""
    var emp:Employee?
    init(name:String) {
        self.name = name
    }
}

class Company{
    var name = ""
    var addr = ""
    init(name:String,addr:String) {
        self.name = name
        self.addr = addr
    }
}

class Employee{
    var name = "Lily"
    var title = "行政"
    var company:Company! //不需要強制解析
    init(name:String,title:String) {
        self.name = name
        self.title = title
    }
    func  info() {
        print("本員工名爲\(self.name),職位是\(self.title))")
    }
}
//第一種方式
//var c = Customer(name:"Damon")
//var emp = Employee(name:"Elena",title:"Student")
//c.emp = emp;
//emp.company = Company(name:"極客學院",addr:"北京市")
//print("爲\(c.name)服務的公司名爲\(c.emp!.company.name)")

//第二種方式 (單獨使用第二種會崩潰,因爲company沒有值)
var c2 = Customer(name: "Lucky")
c2.emp = Employee(name: "Snow", title:"客服")
//print("爲\(c2.name)服務的公司名爲\(c2.emp!.company.name)") 這樣會崩潰
print("爲\(c2.name)服務的公司名爲\(c2.emp?.company?.name)")//用可選鏈

static 在枚舉,結構體中修飾的屬性和方法
class 在類中修飾的屬性和方法
結構體可以包含實例計算屬性,不能包含實例存儲屬性
類中不能定義類型存儲屬性,只能包含類型計算屬性
構造器中常量屬性可以修改

可能失敗的構造器

struct cat {
    let name:String
    init?(name:String) {
        if name.isEmpty {
            return nil
        }
        self.name = name
    }
}
let c1 = cat(name:"Kitty")
if c1 != nil {
    print("C1的name爲:\(c1!.name)")
}
let c2 = cat(name: "")
print(c2)

AnyObject: 可代表任何類的實例
Any:代表任何類

協議:類似於其他語言的接口(多繼承)

protocol Strokable{
    var strokeWidth:Double {get set}
}

protocol Fullable {
    var fullColor: Color?{get set}
}

enum Color{
    case Red,Green,Blue,Yellow,Cyan
}

protocol HasArea : Fullable,Strokable {
    var area: Double {get}
}

protocol Mathable {
    static var pi:Double {get}
    static var e:Double {get}
}

struct Rect:HasArea,Mathable{
    var width:Double
    var height:Double
    init(width:Double,height:Double) {
        self.width = width
        self.height = height
    }
    var fullColor: Color?
    var strokeWidth: Double = 0.0
    var area: Double {
        get{
            return width*height
        }
    }
    static var pi:Double = 3.14159535
    static var e:Double = 2.71828
}
發佈了54 篇原創文章 · 獲贊 32 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章