Classes

class Dog {
    var name = ""
    var age = 0
    
    func tellMeAboutYou() {
        print("The dog's name is \(name) and he is \(age) years old")
    }
}

class Poodle : Dog {
    var furriness = 0
}

var myDog = Dog()
myDog.age = 8
myDog.name = "Miki"

var theOtherDog = Dog()
theOtherDog.age = 10
theOtherDog.name = "Lili"

var dogs : [Dog] = [myDog, theOtherDog]

for dog in dogs {
    myDog.tellMeAboutYou()
}

var myDog2 = Poodle()
myDog2.furriness = 10

print(myDog2.furriness)
Reference:

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/ClassesAndStructures.html

你可能感兴趣的:(Classes)