๐ ๊ฐ์ฒด์ ํด๋์ค - Objects & Classes
ํด๋์ค ์์ฑ ๋ฐฉ๋ฒ
- class ํด๋์ค์ด๋ฆ → ํด๋์ค ์ ์ธ
ํด๋์ค ๋ด๋ถ ์ ์ธ
๊ธฐ์กด์ ์์/๋ณ์/๋ฉ์๋/ํจ์๋ฅผ ์ ์ธํ๋ ๋ฐฉ์๊ณผ ๋์ผํ์ง๋ง ํด๋์ค์ context ๋ด๋ถ์์ ๋ผ๋ ์ ์ด ๋ค๋ฆ
class Shape {
var numberOfSides = 0
func simpleDescription() -> String {
return "A shape with \(numberOfSides) sides."
}
}
ํด๋์ค ์ธ์คํด์ค ์์ฑ
- ํด๋์ค์ด๋ฆ() → ํด๋์ค ์ธ์คํด์ค ์์ฑ
- ํด๋์ค์ด๋ฆ.ํ๋กํผํฐ → ํด๋์ค ํ๋กํผํฐ ์ ๊ทผ
- ํด๋์ค์ด๋ฆ.ํจ์() → ํด๋์ค ํจ์ ์คํ
var shape = Shape()
shape.numberOfSides = 7
var shapeDescription = shape.simpleDescription()
ํด๋์ค init์ ํตํด ์ด๊ธฐํ
class NamedShape {
var numberOfSides: Int = 0
var name: String
init(name: String) {
self.name = name
}
}
์ด๋ self.name์ ์ฃผ๋ชฉํ ํ์๊ฐ ์์
์ด๋ ํด๋์ค์ ํ๋กํผํฐ์ ์ ๊ทผํ๋ ๋ฐฉ์๊ณผ ๋์ผํ ํํ๋ฅผ ๋
์ฆ init์ ํ๋กํผํฐ๋ก ๋ฐ์์ค๋ name๊ณผ ํด๋์ค์ ํ๋กํผํฐ๋ฅผ ๊ตฌ๋ถํ ์ ์๋ ๋ฐฉ๋ฒ์ผ๋ก ์ฌ์ฉ๋ ๊ฒ
- ๋ชจ๋ ํ๋กํผํฐ๋ ํด๋์ค ์ด๊ธฐํ ์ ๊ฐ์ด ํ ๋น๋์ด ์์ด์ผํจ
- ๊ฐ์ฒด๊ฐ ํ ๋น ํด์ ๋๊ธฐ ์ ์ ๋ฆฌ์์ ์ ํ๊ณ ์ถ๋ค๋ฉด deinit์ ํธ์ถํ์ฌ ์์
ํด๋์ค ์์
- SubClass: SuperClass → ํด๋์ค ์์
- ํด๋์ค ์์์ ํ์๊ฐ ์๋ ์ ํ!
- ์์ ํด๋์ค์ ๊ตฌํ์ ์ฌ์ ์ ์ ํ์ ํด๋์ค์ ๋ฉ์๋๋ override๋ฅผ ํ์
ex. SuperClass - NamedShape
class NamedShape {
var numberOfSides: Int = 0
var name: String
init(name: String) {
self.name = name
}
func simpleDescription() -> String {
return "A shape with \(numberOfSides) sides."
}
}
ex. SubClass1 - Square
class Square: NamedShape {
var sideLength: Double
init(sideLength: Double, name: String) {
self.sideLength = sideLength
super.init(name: name)
numberOfSides = 4
}
func area() -> Double {
return sideLength * sideLength
}
override func simpleDescription() -> String {
return "A square with sides of length \(sideLength)."
}
}
let test = Square(sideLength: 5.2, name: "my test square")
print(test.simpleDescription())
// "A square with sides of length \(sideLength)."
print(NamedShape(name: "๋ค์1").simpleDescription())
// "A shape with \(numberOfSides) sides."
→ NamedShape์ simpleDescription์ ์ฌ์ ์ํจ
- ์ฌ์ ์๊ฐ ๋์ด๋ NamedShape ์ธ์คํด์ค์ simpleDescription๋ฅผ ํธ์ถํ๋ฉด ๋์์ ๋์ผ
๋จ์ง Square์์์ simpleDescription๋ฅผ ์ฌ์ ์ํ ๊ฒ
ex. SubClass2 - Circle
class Circle: NamedShape {
func area() -> Double {
// ์๊ด ์์ง๋กฑ
return 0.0
}
override func simpleDescription() -> String {
return "Circle๋ก ์ฌ์ ์ํจ"
}
}
print(Circle(name: "๋๊ทธ๋ผ๋ฏธ").simpleDescription())
// "Circle๋ก ์ฌ์ ์ํจ"
print(Square(sideLength: 5.2, name: "my test square").simpleDescription())
// "A square with sides of length \(sideLength)."
print(NamedShape(name: "๋ค์2").simpleDescription())
// "A shape with \(numberOfSides) sides."
→ NamedShape์ simpleDescription์ ์ฌ์ ์ํจ
- Subclass๋ผ๋ฆฌ๋ override X
SubClass์ init
class EquilateralTriangle: NamedShape {
var sideLength: Double = 0.0
init(sideLength: Double, name: String) {
self.sideLength = sideLength
super.init(name: name)
numberOfSides = 3
}
var perimeter: Double {
get {
return 3.0 * sideLength
}
set {
// setter์ ์์์ ์ด๋ฆ newValue ์ฌ์ฉ
sideLength = newValue / 3.0
}
}
override func simpleDescription() -> String {
return "An equilateral triangle with sides of length \(sideLength)."
}
}
var triangle = EquilateralTriangle(sideLength: 3.1, name: "a triangle")
print(triangle.perimeter)
// Prints "9.3"
triangle.perimeter = 9.9
// set์ newValue์ 9.9๊ฐ ๋ค์ด๊ฐ
print(triangle.sideLength)
// Prints "3.3000000000000003"
init์ ์์
1. SubClass์ ํ๋กํผํฐ ๊ฐ ์ ํ
2. SuperClass์ ์ด๋์ ๋ผ์ด์ ํธ์ถ
3. SuperClass์ ์ํด ์ ์๋ ์์ฑ ๊ฐ์ ๋ณ๊ฒฝํ ์ ์๋ ์์ → SuperClass์ numberOfSides ๊ฐ ๋ณ๊ฒฝ
willSet / didSet์ผ๋ก ๊ฐ ๋ณ๊ฒฝ ์ต์ ๋น
class TriangleAndSquare {
var triangle: EquilateralTriangle {
willSet {
square.sideLength = newValue.sideLength
}
}
var square: Square {
willSet {
triangle.sideLength = newValue.sideLength
}
}
init(size: Double, name: String) {
square = Square(sideLength: size, name: name)
triangle = EquilateralTriangle(sideLength: size, name: name)
}
}
var triangleAndSquare = TriangleAndSquare(size: 10, name: "another test shape")
print(triangleAndSquare.square.sideLength)
// Prints "10.0"
print(triangleAndSquare.triangle.sideLength)
// Prints "10.0"
triangleAndSquare.square = Square(sideLength: 50, name: "larger square") // ํด๋น ์ค์์ ๊ฐ์ ํ ๋นํ๊ธฐ ์ willset์ด ์คํ, ํ ๋น ํ didset์ด ์คํ
print(triangleAndSquare.triangle.sideLength)
// Prints "50.0"
'Swift Language' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
A Swift Tour (1) (0) | 2024.01.25 |
---|---|
Version Compatibility (0) | 2024.01.24 |
About Swift (0) | 2024.01.22 |