0%

Swift 019 Polymorphism and Typecasting

類別繼承

我們可以先創造一個專輯的類別來定義最基本的屬性,之後我們可以將其他各種專輯繼承專輯這個類別,而當我們創建完後,由於都是屬於專輯的類別,因此可以完美且快速的定義在一起。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Album {
var name: String

init(name: String) {
self.name = name
}
}

class StudioAlbum: Album {
var studio: String

init(name: String, studio: String) {
self.studio = studio
super.init(name: name)
}
}

class LiveAlbum: Album {
var location: String

init(name: String, location: String) {
self.location = location
super.init(name: name)
}
}


var apple = StudioAlbum(name: "Apple", studio: "Pie")
var banana = StudioAlbum(name: "Banana", studio: "ship")
var pear = LiveAlbum(name: "Pear", location: "taiwan")
var allAlbums: [Album] = [apple, banana, pear]

我們可以透過定義getPerformance之後再使用override進行修改,之後我們可以快速的將所有的getPerformance快速的列印出來。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class AlbumGod {
var name: String

init(name: String) {
self.name = name
}

func getPerformance() -> String {
return "The album is \(name)"
}
}

class StudioAlbumGod: AlbumGod {
var studio: String

init(name: String, studio: String) {
self.studio = studio
super.init(name: name)
}

override func getPerformance() -> String {
return "The studio album is \(name)"
}
}

class LiveAlbumGod: AlbumGod {
var location: String

init(name: String, location: String) {
self.location = location
super.init(name: name)
}

override func getPerformance() -> String {
return "The live album is \(name)"
}
}


var appleGod = StudioAlbumGod(name: "Apple", studio: "Pie")
var bananaGod = StudioAlbumGod(name: "Banana", studio: "ship")
var pearGod = LiveAlbumGod(name: "Pear", location: "taiwan")
var allAlbumGods: [AlbumGod] = [appleGod, bananaGod, pearGod]

for albumGod in allAlbumGods {
print(albumGod.getPerformance())
}