您的位置:首页 > 移动开发 > Swift

iOS讲解迷惑--Swift中枚举、继承、协议

2015-12-12 22:55 676 查看
//: Playground - noun: a place where people can play

import UIKit

var str = "Hello, playground"

// 枚举
//enum 枚举名: 类型 {
//    case 分支1 = 赋值1
//    case 分支2 = 赋值2
//}

enum PersonIndentity: String {
    case Teacher = "Teacher_id"
    case Student = "Student_id"
}

class Person {
    
    // 属性
    var indetity: PersonIndentity?
    var name: String?
    var age: String
    
    
    // 类的构造器(也就是初始化方法)
    init(name: String, age:String, idd: PersonIndentity) {
        self.name = name
        self.age = age
        self.indetity = idd
    }
    
    
    // 方法
    func hello(){
        print("hello")
    }
    
    // 类型方法
    class func hello2(){
        
    }
    
}

// 枚举  枚举名.类型
// 例如 PersonIndentity.Student
// PersonIndentity.Student 枚举名可以省略(利用类型推断)
var person = Person(name: "毛毛", age: "80", idd: /*PersonIndentity*/.Student) // 创建person对象

// 枚举值的获取
// 获取当前分支
person.indetity!.hashValue // 1
person.indetity?.hashValue // 1

// 获取分支的值
person.indetity?.rawValue // "Student_id"

// 继承 类名:父类名
// Student继承Person
class Student: Person {
    
    var classNumber: Int
    init(name: String, age: String, idd: PersonIndentity, classNum:Int) {

        // 如果属性仅指定类型, 需要在super.init前进行赋值操作
        self.classNumber = classNum
        super.init(name: name, age: age, idd: idd)
        self.classNumber
    }
    
    // 重写父类方法需要使用override关键字
    override func hello() {
        
    }
    
    
    // 重写类型方法
    override class func hello2() {
        
    }
    
}

//*****************************
//***************************** 协议
protocol OneProtocol {
    func typeFunc1()
    static func typeFunc2() // 加了static相当于类方法
    mutating func typeFunc3()
}

// 写个类 继承协议OneProtocol
// 类 和 结构体实现协议方法的时候需要根据本身的语法规则做出调整
class StudentNew:OneProtocol {
    
    func typeFunc1() {
        
    }
    
    // 这里要将 static变成class, 否则报错继承协议的地方会报错
    class func typeFunc2() {
        
    }
    
    func typeFunc3() {
        
    }
    
    
}

StudentNew.typeFunc2()

// 写个结构体 继承 OneProtocol协议
struct VideoNew: OneProtocol {
    func typeFunc1() {
        
    }
    
    static func typeFunc2() {
        
    }
    
     mutating func typeFunc3() {
        
    }
    
}

// 类 同时继承父类 和 协议的时候, 父类必须写在前面
//class StudentNew2:Person, OneProtocol {
//    
//}

// @objc
@objc protocol TwoProtocol {
  optional  func bye()
    
}

// 此处报错, 因为上面的的协议用了 @objc 修饰
// 原因: 当协议中方法使用 optional声明可选时, 协议必须声明成@objc, 此时协议为类型协议:class protocol, 并且不能被结构体继承
//struct Struct2: TwoProtocol {
//    
//}

//*****************************
// ************************* 扩展
var value: String = ""
extension Person {
    
    // 扩展一个方法
    func hello5(){
        
    }
    
    
    // 如果想向扩展属性只能是计算属性
    var stu2: String {
        get {
            return value
        }
        set {
            value = newValue
        }
    }
    
    
    
    // 类的构造器(也就是初始化方法)
    ///  扩展构造器 的时候要使用convenience
    /// 多加一个参数 stu2:
    convenience init(name: String, age:String, idd: PersonIndentity, stu2:String) {
        
        /// 重新调用自己的原来的方法
        self.init(name:name, age:age, idd:idd)
        
        /// 给多加的参数赋值
        self.stu2 = stu2
    }
}

/// 多了一个参数stu2:
var person2 = Person(name: "毛毛扩展", age: "100", idd: .Student, stu2:"笨蛋")

person2.stu2
person2.stu2 = "笨蛋蛋"
person2.stu2
person2.name

class Animal{
    var type: String = ""
    var color: String = ""
    
    func play(){
        print("动物都爱吃")
    }
    
    
    
    init(type:String, color:String) {
        self.type = type
        self.color = color
    }
    
    
    var type2:String{
        
        get{
            return self.type
        }
        set{
            self.type = newValue
        }
        
    }
    
    
    
    
}

let animal = Animal(type: "陆地", color: "黑色")
animal.color = "白色"
animal.color

animal.type2 = "水生" // 调用set方法
animal.type2  //  调用get方法 "水生"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: