美文网首页
Swift 2 学习笔记 22.类型检查和类型转换

Swift 2 学习笔记 22.类型检查和类型转换

作者: Maserati丶 | 来源:发表于2018-12-03 21:51 被阅读0次

    课程来自慕课网liuyubobobo老师


    类型检查和类型转换
    • 类型检查
    let library: [MediaItem] = [
        Movie(name: "Zootopia", genre: "Animation"),
        Music(name: "Hello", artistName: "Adele"),
        Game(name: "LIMBO", developer: "Playdead"),
        Music(name: "See you agian", artistName: "Wiz Khalifa"),
        Game(name: "The Bridge", developer: "The Quantum Astrophysicists Guild")
    ]
    
    for mediaItem in library{
        if mediaItem is Movie{
            movieCount += 1
        }
        else if mediaItem is Music{
            musicCount += 1
        }
        else if mediaItem is Game{
            gameCount += 1
        }
    }
    
    • 向下类型转换
    //let item0 = library[0] as? Movie
    //let item0 = library[0] as? Music
    //let item0 = library[0] as! Movie
    //let item0 = library[0] as! Music
    
    for mediaItem in library{
        if let movie = mediaItem as? Movie{
            print("Movie:", movie.name, "Genre:", movie.genre)
        }
        else if let music = mediaItem as? Music{
            print("Music:", music.name, "Artist:", music.artist)
        }
        else if let game = mediaItem as? Game{
            print("Game:", game.name, "Developer(s):", game.developer)
        }
    
    • 检查协议遵守
    protocol Shape{
        var name: String{get}
    }
    
    protocol HasArea{
        func area() -> Double
    }
    
    
    struct Point: Shape{
        let name: String = "point"
        var x: Double
        var y: Double
    }
    
    struct Rectangle: Shape, HasArea{
        let name: String = "rectangle"
        var origin: Point
        var width: Double
        var height: Double
        
        func area() -> Double{
            return width * height
        }
    }
    
    struct Circle: Shape, HasArea{
        let name = "circle"
        var center: Point
        var radius: Double
        
        func area() -> Double{
            return Double.pi * radius * radius
        }
    }
    
    struct Line: Shape{
        let name = "line"
        var a: Point
        var b: Point
    }
    
    let shapes:[Shape] = [
        Rectangle(origin: Point(x:0.0,y:0.0), width: 3.0, height: 4.0),
        Point(x: 0.0, y: 0.0),
        Circle(center: Point(x:0.0,y:0.0), radius: 1.0),
        Line(a: Point(x:1.0,y:1.0), b: Point(x:5.0,y:5.0))
    ]
    
    for shape in shapes{
        
        if shape is HasArea{
            print("\(shape.name) has area.")
        }
        else{
            print("\(shape.name) has no area.")
        }
    }
    
    print("==========")
    
    for shape in shapes{
        
        if let areaShape = shape as? HasArea{
            print("The area of \(shape.name) is \(areaShape.area()).")
        }
        else{
            print("\(shape.name) has no area.")
        }
    }
    

    相关文章

      网友评论

          本文标题:Swift 2 学习笔记 22.类型检查和类型转换

          本文链接:https://www.haomeiwen.com/subject/hhoucqtx.html