Search
 
SCRIPT & CODE EXAMPLE
 

SWIFT

abstract class in swift

class Animal {
    func sound() {}
}

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

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

Abstract classes 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
Comment

PREVIOUS NEXT
Code Example
Swift :: swift view controller background image 
Swift :: Swift Explicitly declaring an unwrapped optional 
Swift :: swiftui slide menu 
Swift :: Swift Find Number of Dictionary Elements 
Swift :: swift constraints 
Swift :: string swift 
Swift :: swift ease in out animatekeyframes 
Swift :: Assign values to enum variables Swift 
Swift :: Speech recognizer swiftui 
Swift :: how to read music library from iphone programmatically in swift 
Swift :: AMAZONCONNECT 
Swift :: octobercms add extra slash to css url 
Swift :: Swift String Example 
Swift :: string to decimal swift 
Swift :: xcode combine calayer into an image 
Ruby :: how to rename a table in ruby 
Ruby :: rails image tag 
Ruby :: get current year in ruby 
Ruby :: rspec parallel tests 
Ruby :: ruby loop through array from last item backwards 
Ruby :: ruby check if a file exists 
Ruby :: function is uninitialized constant ruby 
Ruby :: ruby square root 
Ruby :: shopify: how to show percentage discount saved 
Ruby :: rails rescue puts error 
Ruby :: rails generate controller no respec 
Ruby :: read headers of csv using ruby 
Ruby :: input must be integer in ruby 
Ruby :: ruby join hash to string 
Ruby :: ruby array loop 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =