๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Swift Language

A Swift Tour(2)

๐ŸŒˆ ๊ฐ์ฒด์™€ ํด๋ž˜์Šค - 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"

 

728x90

'Swift Language' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

A Swift Tour (1)  (0) 2024.01.25
Version Compatibility  (0) 2024.01.24
About Swift  (0) 2024.01.22