“Abstrakte Klassen in Swift” Code-Antworten

abstrakte Klasse in Swift

class Animal {
    func sound() {}
}

class Cat: Animal {
    override func sound() {
        print("miauw")
    }
}

class Dog: Animal {
    override func sound() {
        print("woof")
    }
}
Jack Sparrow

Abstrakte Klassen in Swift

protocol Employee {
    var annualSalary: Int {get}
}

extension Employee {
    var biweeklySalary: Int {
        return self.annualSalary / 26
    }

    func logSalary() {
        print("$\(self.annualSalary) per year or $\(self.biweeklySalary) biweekly")
    }
}

struct SoftwareEngineer: Employee {
    var annualSalary: Int

    func logSalary() {
        print("overridden")
    }
}

let sarah = SoftwareEngineer(annualSalary: 100000)
sarah.logSalary() // prints: overridden
(sarah as Employee).logSalary() // prints: $100000 per year or $3846 biweekly
Jack Sparrow

Ähnliche Antworten wie “Abstrakte Klassen in Swift”

Fragen ähnlich wie “Abstrakte Klassen in Swift”

Weitere verwandte Antworten zu “Abstrakte Klassen in Swift” auf Swift

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen