swift 面向对象的Swift

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift 面向对象的Swift相关的知识,希望对你有一定的参考价值。

let x1 = 0
let y1 = 0

let coordinate1: (x: Int, y: Int) = (0, 0)
coordinate1.x

struct Point {
    // Stored Properties
    let x: Int
    let y: Int
    
    init(a: Int, b:Int) {
        self.x = a
        self.y = b
    }
    
    /// Returns the surrounding points in range of
    /// the current one
    func points(inRange range: Int = 1) -> [Point] {
        var results = [Point]()
        
        let lowerBoundOfXRange = x - range
        let upperBoundOfXRange = x + range

        let lowerBoundOfYRange = y - range
        let upperBoundOfYRange = y + range
        
        for xCoordinate in lowerBoundOfXRange...upperBoundOfXRange {
            for yCoordinate in lowerBoundOfYRange...upperBoundOfYRange {
                let coordinatePoint = Point(a: xCoordinate, b: yCoordinate)
                results.append(coordinatePoint)
            }
        }
        
        
        return results
    }
}

let coordinatePoint = Point(a: 1, b: 1) // Instance

coordinatePoint.points()

// Enemies

class Enemy {
    var life: Int = 2
    let position: Point
    
    init(x: Int, y: Int) {
        self.position = Point(a: x, b: y)
        
    }
    
    func decreaseLife(by factor: Int){
        life -= factor
    }
    
}

class SuperEnemy: Enemy {
    let isSuper: Bool = true
    
    override init(x: Int, y: Int) {
        super.init(x: x, y: y)
        self.life = 50
    }
}


// Towers

class Tower {
    let position: Point
    var range: Int = 1
    var strength: Int = 1
    
    init(x: Int, y: Int) {
        self.position = Point(a: x, b: y)
    }
    
    func fire(at enemy: Enemy) {
        if isInRange(of: enemy) {
            enemy.decreaseLife(by: strength)
            print("Gotcha")
        } else {
            print("Darn! Out of range!")
        }
    }
    
    func isInRange(of enemy: Enemy) -> Bool {
        let availablePositions = position.points(inRange: range)
        
        for point in availablePositions {
            if point.x == enemy.position.x && point.y == enemy.position.y {
                return true
            }
        }
        return false
    }
}

class LaserTower: Tower {
    
    override init(x: Int, y: Int) {
        super.init(x: x, y: y)
        self.range = 100
        self.strength = 100
    }
    
    override func fire(at enemy: Enemy) {
        while enemy.life >= 0 {
            enemy.decreaseLife(by: strength)
        }
        print("Enemy destroyed!")
    }
    
}


let tower = Tower(x: 0, y: 0)
let enemy = Enemy(x: 1, y: 1)
let superEnemy = SuperEnemy(x: 1, y: 1)
let laserTower = LaserTower(x: 2, y: 2)



tower.fire(at: enemy)
laserTower.fire(at: superEnemy)



class Person {
    let firstName: String
    let lastName: String
    
    init(firstName: String, lastName: String) {
        self.firstName = firstName
        self.lastName = lastName
    }
    
    func fullName() -> String {
        return "\(firstName) \(lastName)"
    }
}

// Enter your code below

class Doctor: Person {
    
    /*override init(firstName: String, lastName: String) {
        super.init(firstName: firstName, lastName: lastName)
    }*/
    
    override func fullName() -> String {
        return "Dr. \(lastName)"
    }
}

let someDoctor = Doctor(firstName: "Mustafa", lastName: "Kaya")
let somePerson = Person(firstName: "Mustafa", lastName: "Kaya")

someDoctor.fullName()
somePerson.fullName()

以上是关于swift 面向对象的Swift的主要内容,如果未能解决你的问题,请参考以下文章

Swift学习(3面向对象)

Swift 中的面向协议编程:是否优于面向对象编程?

为什么swift是面向协议的编程--对面向对象机制的改进

Swift中面向协议的编程

数据结构 AOV网的关键路径(Swift面向对象版)

涉及 JSON 解析以及静态和动态 UITableView 单元格的 Swift3 项目 - 使其更加面向对象