-
Notifications
You must be signed in to change notification settings - Fork 0
/
OOP.swift
214 lines (169 loc) · 4.81 KB
/
OOP.swift
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//
// OOP.swift
// ProtocolOrientedProgramming
//
// Created by A on 2021/02/16.
//
import Foundation
/// 객체지향 프로그래밍
enum TerrainType {
case land
case sea
case air
}
// 이동수단의 슈퍼클래스
class Vehicle {
fileprivate var vehicleTypes = [TerrainType]()
fileprivate var vehicleAttackTypes = [TerrainType]()
fileprivate var vehicleMovementTypes = [TerrainType]()
fileprivate var landAttackRange = -1
fileprivate var seaAttackRange = -1
fileprivate var airAttackRange = -1
fileprivate var hitPoints = 0
func isVehicleType(type: TerrainType) -> Bool {
return vehicleTypes.contains(type)
}
func canVehicleAttack(type: TerrainType) -> Bool {
return vehicleAttackTypes.contains(type)
}
func canVehicleMove(type: TerrainType) -> Bool {
return vehicleMovementTypes.contains(type)
}
func doLandAttack() {}
func doLandMovement() {}
func doSeaAttack() {}
func doSeaMovement() {}
func doAirAttack() {}
func doAirMovement() {}
func takeHit(amount: Int) { hitPoints -= amount }
func hitPointsRemaining() -> Int { return hitPoints }
func isAlive() -> Bool { return hitPoints > 0 ? true : false }
}
// land type 서브클래스
class Tank: Vehicle {
override init() {
super.init()
vehicleTypes = [.land]
vehicleAttackTypes = [.land]
vehicleMovementTypes = [.land]
landAttackRange = 5
hitPoints = 68
}
override func doLandAttack() {
print("Tank Attack")
}
override func doLandMovement() {
print("Tank Move")
}
}
// land & sea type 서브클래스
class Amphibious: Vehicle {
override init() {
super.init()
vehicleTypes = [.land, .sea]
vehicleAttackTypes = [.land, .sea]
vehicleMovementTypes = [.land, .sea]
landAttackRange = 1
seaAttackRange = 1
hitPoints = 25
}
override func doLandAttack() {
print("Amphibious Land Attack")
}
override func doLandMovement() {
print("Amphibious Land Move")
}
override func doSeaAttack() {
print("Amphibious Sea Attack")
}
override func doSeaMovement() {
print("Amphibious Sea Move")
}
}
// land & sea & air type 서브클래스
class Transformer: Vehicle {
override init() {
super.init()
vehicleTypes = [.land, .air, .sea]
vehicleAttackTypes = [.land, .air, .sea]
vehicleMovementTypes = [.land, .air, .sea]
landAttackRange = 7
seaAttackRange = 10
airAttackRange = 12
hitPoints = 75
}
override func doLandAttack() {
print("Tranformer Land Attack")
}
override func doLandMovement() {
print("Tranformer Land Move")
}
override func doSeaAttack() {
print("Tranformer Sea Attack")
}
override func doSeaMovement() {
print("Tranformer Sea Move")
}
override func doAirAttack() {
print("Transformer Air Attack")
}
override func doAirMovement() {
print("Tranformer Air Move")
}
}
var vehicles = [Vehicle]()
var vh1 = Amphibious()
var vh2 = Amphibious()
var vh3 = Tank()
var vh4 = Transformer()
vehicles.append(vh1)
vehicles.append(vh2)
vehicles.append(vh3)
vehicles.append(vh4)
for (index, vehicle) in vehicles.enumerated() {
if vehicle.isVehicleType(type: .air) {
print("Vehicle at \(index) is Air")
if vehicle.canVehicleAttack(type: .air) {
vehicle.doAirAttack()
}
if vehicle.canVehicleMove(type: .air) {
vehicle.doAirMovement()
}
}
if vehicle.isVehicleType(type: .land) {
print("Vehicle at \(index) is Land")
if vehicle.canVehicleAttack(type: .land) {
vehicle.doLandAttack()
}
if vehicle.canVehicleMove(type: .land) {
vehicle.doLandMovement()
}
}
if vehicle.isVehicleType(type: .sea) {
print("Vehicle at \(index) is Sea")
if vehicle.canVehicleAttack(type: .sea) {
vehicle.doSeaAttack()
}
if vehicle.canVehicleMove(type: .sea) {
vehicle.doSeaMovement()
}
}
}
/// 객체지향 설계 방식의 문제점
class Infantry: Vehicle {
override init() {
super.init()
vehicleTypes = [.land]
vehicleAttackTypes = [.land]
vehicleMovementTypes = [.sea]
landAttackRange = 1
seaAttackRange = 1
hitPoints = 25
}
override func doLandAttack() {
print("Amphibious Land Attack")
}
override func doAirMovement() {
print("Amphibious Land Move")
}
}