Swift学习第二天--面向对象

//: Playground - noun: a place where people can play

import UIKit

var str = "Hello, playground"


//-------------------- 类和属性 --------------------

class Role{
    
    //成员属性
    var id = ""
    var money = 0
    
    //静态属性
    static var method = "RNA"
    
    //子类可以重写的静态属性
    class var url :String {
        return "Malcolm Feng"
    }
    
    //成员方法
    func playgame() -> String{
        return "playing....(*&^%$#$%^&*(GFR&*"
    }
    //静态方法
    static func playboll() -> String{
        return "playing boll.......23456789"
    }
    //子类可重写的静态方法
    class func playwater() -> String{
        return "playing water......aliuehflkauweflk"
    }
    
}

// 1.调用成员属性
let hero = Role()
hero.id = "123456789"
hero.money = 5000
hero.id

// 2.调用成员方法
hero.playgame()

// 3.调用静态属性
Role.method
Role.url

// 4.调用静态方法
Role.playwater()
Role.playboll()






//-------------------- 类的继承 --------------------

class Myrole :Role{
    var method :String = "DNA"
    override class var url :String {  //重写父类静态属性
        return "FengDianLong"
    }
    
    override func playgame() -> String{
        return "play game by child class"
    }
    
    override static func playwater() -> String{
        return "playing water by child class"
    }

}

Myrole.method
Myrole.url
Myrole.playwater()
Myrole.playboll()





//-------------------- 结构体和属性 --------------------
struct Me{
    static var name = "Malcolm"
    static var url = "http://malcolm.cn"
    var thanks = ""
    var information :String {
        return Me.name + " 的网站:" + Me.url + thanks
    }
}
//调用静态属性
print(Me.name)

//调用成员属性
var me = Me(thanks:"谢谢光临")
print(me.information)

//for i in Array(1...100){
//    print(i)
//}
//print("1")



//-------------------- 下标 --------------------

//常见的下标
var array = ["feng","dian","long"]
array[1]

var dict = ["name":"fengdianlong", "age":"24", "langruage":"swift"]
dict["name"]

struct CycleArea{
    func area(radius: Double) -> Double {
        return Double.pi * pow(radius, 2)
    }
    subscript(radius : Double) -> Double{
        return Double.pi * pow(radius, 2)
    }
}

//调用方法
var cycleArea = CycleArea()
cycleArea.area(radius: 3)

//调用subscript
cycleArea[3]


//-------------------- 初始化 --------------------

//**********类 的参数初始化
class Room {
    var season : String
    var temp : Int
    
    init(season: String, temp:Int) {
        self.season = season
        self.temp = temp
    }
}
let room = Room(season: "spring", temp:32)
room.season
room.temp

//**********结构体 的参数初始化
struct RoomStruct{
    var season : String
    var temp : Int
}

let roomStruct = RoomStruct(season : "summer", temp : 34)
roomStruct.season
roomStruct.temp

//**********便利构造器
class Food{
    var name :String
    
    init(name :String) {
        self.name = name
    }
    
    convenience init(){
        self.init(name: "水果")
    }
}

let food = Food()
food.name

//**********可失败构造器

struct Animal{
    var name : String
    
    init?(name : String) {
        if name.isEmpty{
            print("没有给动物取名哦")
            return nil
        }
        self.name = name
    }
}

let cat = Animal(name:"")
cat?.name

let dog = Animal(name:"狗狗")
dog?.name


//-------------------- 异常 --------------------
//创建异常枚举
enum LearningObs : Error{
    case noMethod, noReading, noTool(tool : String)
}

//创建学习ios开发的方法
func iosDev(method: Bool, style : Bool, hasTool : Bool) throws{
    guard method else {
        throw LearningObs.noMethod
    }
    guard style else {
        throw LearningObs.noReading
    }
    guard hasTool else {
        throw LearningObs.noTool(tool: "缺Mac电脑")
    }
}

//买电脑
var budget = 7000

func buy(tool:String){
    if budget >= 6000 {
        budget -= 6000
        print("您已经购买Mac电脑")
    } else {
        print("资金不足")
    }
}

//调用学习ios开发的方法  捕获异常 进行处理
do{
    try iosDev(method: false, style: false, hasTool: false)
    print("开始学习IOS开发")
}catch LearningObs.noMethod {
    print("没有好的学习方法")
}catch LearningObs.noReading {
    print("不想看书")
}catch LearningObs.noTool(let tool) {
    print("没有mac电脑")
    buy(tool: "mac")
}


//********* 不用关注异常的细节,可以使用 try?  try!
if let result = try? iosDev(method: true, style: true, hasTool: true){
    print("恭喜进入学习")
}else{
    print("学习条件不足")
}

 

posted @ 2017-06-15 17:12  MalcolmFeng  阅读(191)  评论(0编辑  收藏  举报