首页 > 代码库 > Swift初体验(三)

Swift初体验(三)

/*******************************************************************************/
//  协议
protocol Description{
    
    func description() -> String
    mutating func aMutatingMethod()
    class func aTypeMethod()
//    func anOptionalMethod()
}

//  如果要声明可选择实现的方法,需要在func前加@optional声明,而且协议的前面需要加@objc声明,这样的协议结构体不能遵守,所以也不允许mutating方法的存在
//@objc protocol Description{
//    
//    func description() -> String
////    mutating func aMutatingMethod()
//    class func aTypeMethod()
//    @optional func anOptionalMethod()
//}

//  结构体
struct Point{
    var x = 0.0
    var y = 0.0
}

struct Size{
    var width = 0.0
    var height = 0.0
}

var point = Point()
var size = Size()

var array = Array<String>()

struct Rect{
    var origin = Point()
    var size   = Size()
    var center: Point{
    get{
        let centerX = origin.x + size.width / 2
        let centerY = origin.y + size.height / 2
        return Point(x:centerX , y:centerY)
    }
    set{
        //  set方法会提供一个默认的新值:newValue
        let originX = newValue.x - size.width / 2
        let originY = newValue.y - size.height / 2
        origin = Point(x:originX, y:originY)
    }
    }
    
    mutating func moveBy(deltaX:Double, deltaY:Double){
        let newX = origin.x + deltaX
        let newY = origin.y + deltaY
        origin = Point(x:newX, y:newY)
    }
    
    func description() -> String{
        return "(\(origin.x),\(origin.y),\(size.width),\(size.height))"
    }
    
    mutating func aMutatingMethod(){
    
    }
    
    func anOptionalMethod(){
    
    }
    
    //  如果是实现协议中得类方法,虽然在协议中使用了class关键字,但是在结构体或者枚举实现的时候依然需要使用static关键字
    static func aTypeMethod(){
    
    }
    
}
var frame = Rect()
frame.origin.x = 10
frame.origin.y = 10
frame.size = Size(width:100,height:100)

println("frame.center = (\(frame.center.x), \(frame.center.y))")

frame.center = Point(x:100,y:100)
println("frame.center = (\(frame.center.x), \(frame.center.y))")
println("frame.origin = (\(frame.origin.x), \(frame.origin.y))")

//  枚举
enum PersonIdentify:String{
    case Teacher = "teacher"
    case Student = "student"
}

//  类
class Person{
    var name: String?
    var sex:  String?
    var identify: PersonIdentify?
    //  构造器中,第一个参数依然会创建一个和局部参数名称相同的外部参数名称
    init(name: String, sex: String, identify: PersonIdentify){
    
        self.name = name;
        self.sex = sex;
        self.identify = identify
    }
    
    func description() -> String{
        //  调用枚举值的toRaw()方法可以获取到枚举值的初值
        return "name = \(name) sex = \(sex) course = \(identify!.toRaw())"
    }
    
    //  在类中如果实现协议中定义的mutating方法,那么不需要加"mutating"关键字
    func aMutatingMethod(){
    
    }
    
    class func aTypeMethod(){
        
    }
}

var person1 = Person(name:"humingtao",sex:"m",identify:.Student)
var person2 = Person(name:"kebi",sex:"m",identify:.Teacher)
switch(person1.identify!){
case .Teacher:
    println("I am a teacher")
case .Student:
    println("student")
}

class Student:Person{
    var number: String?
    //  在类中,类型属性只可以是计算属性,但是在枚举和结构体中,既可以是计算属性,也可以是存值属性
    //  在类中,声明一个类型属性的关键字是class,在结构体和枚举中是static
    class var attendClassTime: String{
        return "9:30"
    }
    init(name: String, sex: String, number: String){
    
        super.init(name: name, sex: sex, identify: .Student)
        self.number = number
    }
}
var student1 = Student(name: "hmt",sex: "m",number: "12345678")

class Teacher:Person{
    var course: String?
    init(name: String, sex: String, course: String){
    
        super.init(name: name, sex: sex, identify: .Teacher)
        self.course = course
    }
    override func description() -> String{
    
        return super.description() + "" + "course = \(course)"
    }
}

//  思考为什么加"?"
class course{
    var name: String?
    init(name: String){
    
        self.name = name
    }
}