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
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值