Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Swift/Swift BASIC

[Swift] ๊ธฐ์ดˆ ๋ฌธ๋ฒ• - ์ƒ์† (Inheritance) - sub class, super class๋ž€?

๊ฐ์ž ๐Ÿฅ” 2022. 1. 17. 14:53
๋ฐ˜์‘ํ˜•

 

์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์ž์ž…๋‹ˆ๋‹ค. ์•ž์—์„œ ํด๋ž˜์Šค์— ๋Œ€ํ•œ ๊ฐœ๋…์„ ๊ณต๋ถ€ํ•  ๋•Œ, class์™€ struct์˜ ์ฐจ์ด์ ์ค‘ ํด๋ž˜์Šค๋Š” '์ƒ์†์ด ๊ฐ€๋Šฅํ•˜๋‹ค'๋ผ๋Š” ๊ฐœ๋…์ด ์žˆ์—ˆ๋Š”๋ฐ์š”! ์ด์— ๋Œ€ํ•œ ๊ฐœ๋…์„ ์ž์„ธํžˆ ์‚ดํŽด๋ณด๋„๋ก ํ•ฉ์‹œ๋‹ค. class์™€ sruct์˜ ์ฐจ์ด์  ๋ณด๋Ÿฌ๊ฐ€๊ธฐ click

์•™ ๊ท€์—ฌ์›Œ

๋ณธ ํฌ์ŠคํŒ…์€ ํŒจ์ŠคํŠธ์บ ํผ์Šค์˜ 30๊ฐœ ํ”„๋กœ์ ํŠธ๋กœ ๋ฐฐ์šฐ๋Š” iOS์•ฑ๊ฐœ๋ฐœ with Swift ๊ฐ•์˜๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.

๋ฐ˜์‘ํ˜•
 

ํŒจ์ŠคํŠธ์บ ํผ์Šค [์ง์žฅ์ธ ์‹ค๋ฌด๊ต์œก]

ํ”„๋กœ๊ทธ๋ž˜๋ฐ, ์˜์ƒํŽธ์ง‘, UX/UI, ๋งˆ์ผ€ํŒ…, ๋ฐ์ดํ„ฐ ๋ถ„์„, ์—‘์…€๊ฐ•์˜, The RED, ๊ตญ๋น„์ง€์›, ๊ธฐ์—…๊ต์œก, ์„œ๋น„์Šค ์ œ๊ณต.

fastcampus.co.kr

 


 

์ƒ์†์ด๋ž€? (๋ถ€๋ชจํด๋ž˜์Šค์™€ ์ž์‹ํด๋ž˜์Šค, sub class, super class)

๋ถ€๋ชจ๊ฐ€ ์ž์‹์—๊ฒŒ ์žฌ์‚ฐ์„ ๋ฌผ๋ ค์ฃผ๋Š” ํ–‰์œ„๋ฅผ ์˜๋ฏธํ•œ๋‹ค. swift์—์„œ๋Š” ์ด ๊ฐœ๋…์„ ํ™œ์šฉํ•˜์—ฌ class๊ฐ€ ๋‹ค๋ฅธ class๋กœ๋ถ€ํ„ฐ ๋ฉ”์„œ๋“œ, ํ”„๋กœํผํ‹ฐ ๋“ฑ์„ ์ƒ์†๋ฐ›๋Š”๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค. ํ•œ ํด๋ž˜์Šค๊ฐ€ ๋‹ค๋ฅธ ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ• ๋•Œ ์ƒ์† ๋ฐ›๋Š” ํด๋ž˜์Šค๋ฅผ ์ž์‹ํด๋ž˜์Šค (sub-class)๋ผ๊ณ  ํ•˜๊ณ , ์ƒ์† ํ•ด์ฃผ๋Š” ํด๋ž˜์Šค๋ฅผ ๋ถ€๋ชจํด๋ž˜์Šค (super-class) ๋ผ๊ณ  ํ•œ๋‹ค.

swift ํด๋ž˜์Šค๋Š” ๋ถ€๋ชจ ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ๋ฌผ๋ ค๋ฐ›์€ ๋ฉ”์„œ๋“œ, ํ”„๋กœํผํ‹ฐ, ์„œ๋ฒ„์Šคํฌ๋ฆฝํŠธ๋ฅผ ํ˜ธ์ถœํ•˜๊ฑฐ๋‚˜ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. overriding๋ฅผ ํ†ตํ•ด ๋ฌผ๋ ค๋ฐ›์€ ๋ฉ”์„œ๋“œ, ํ”„๋กœํผํ‹ฐ, ์„œ๋ธŒ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž์‹ ๋งŒ์˜ ๊ฒƒ์œผ๋กœ ์žฌ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ƒ์†๋ฐ›์€ ํ”„๋กœํผํ‹ฐ์˜ ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜์—ˆ์„ ๋•Œ ํ”„๋กœํผํ‹ฐ ๊ฐ์‹œ์ž๋„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ์—ฐ์‚ฐ ํ”„๋กœํผํ‹ฐ์™€ ์ €์žฅํ”„๋กœํผํ‹ฐ๋Š” ์ž์‹ ํด๋ž˜์Šค์—์„œ ํ”„๋กœํผํ‹ฐ ๊ฐ์‹œ์ž๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค.

 

โ–ถ base class (๋ฒ ์ด์Šค ํด๋ž˜์Šค)

๋‹ค๋ฅธ ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ์ƒ์†๋ฐ›์ง€ ์•Š๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ Vehicle class ๋Š” ๋ฒ ์ด์Šค ํด๋ž˜์Šค ๋ผ๊ณ  ํ•œ๋‹ค. 

class Vehicle {
	var currentSpeed = 0.0
    var description: String {
    	return "traveling at \(currentSpeed) miles per hour"
	}
    func makeNoise() {
    }
}

 

โ–ถ ์„œ๋ธŒ ํด๋ž˜์Šค (sub class) 

์ƒ์† ๋ฐ›๋Š” ํด๋ž˜์Šค์˜ ํ˜•ํƒœ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

class ํด๋ž˜์Šค์ด๋ฆ„: ๋ถ€๋ชจํด๋ž˜์Šค ์ด๋ฆ„ {
	// ํ•˜์œ„ ํด๋ž˜์Šค ์ •์˜
}

์•ž์— ์žˆ๋Š” Vehicle ์„ ๋ถ€๋ชจ ํด๋ž˜์Šค๋กœ ๊ฐ–๋Š” ์„œ๋ธŒ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด๋ณด์ž. 

class Bicycle: Vehicle {
	var hasBasket = false
}

์ด๋ ‡๊ฒŒ ํ•˜๊ฒŒ ๋˜๋ฉด, ์‹ค์ œ๋กœ Bicycle์ด๋ผ๋Š” ํด๋ž˜์Šค ๋‚ด๋ถ€์—๋Š” ํ•˜๋‚˜์˜ ๋ณ€์ˆ˜๋งŒ ์กด์žฌํ•˜๋Š” ๊ฒƒ ๊ฐ™์ง€๋งŒ, Vehicle ์—์„œ ์ •์˜ํ•œ ํ”„๋กœํผํ‹ฐ์™€ ๋ฉ”์„œ๋“œ๋ฅผ ๋ชจ๋‘ ์ƒ์† ๋ฐ›๊ฒŒ ๋œ๋‹ค. ์ฆ‰ ๋ถ€๋ชจํด๋ž˜์Šค ๋‚ด๋ถ€์— ์žˆ๋Š” ๋ชจ๋“  ํ”„๋กœํผํ‹ฐ, ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์–˜๊ธฐ๋‹ค. ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ์Šˆํผํด๋ž˜์Šค์— ์žˆ๋Š” ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•ด๋ณด์ž.

var bicycle = Bicycle()
bicycle.currentSpeed
// prints "0"
// Vehicle์—์„œ ์ •์˜ํ•œ ์ดˆ๊ธฐ currentspeed๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค

bicycle.currentSpeed = 12.0
bicycle.currentSpeed
// prints "12.0"

 

Overriding ์ด๋ž€? (์˜ค๋ฒ„๋ผ์ด๋”ฉ)

sub class๋Š” super class์—์„œ ์ •์˜๋œ ๋ชจ๋“  ๊ฒƒ๋“ค์„ ๊ทธ๋ƒฅ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  , ์ž์‹ ๋งŒ์˜ ๊ฐœ๋…์œผ๋กœ ์žฌ์ •์˜ํ•ด์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. ์ด๊ฒƒ์„ ์˜ค๋ฒ„๋ผ์ด๋”ฉ์ด๋ผ๊ณ  ํ•œ๋‹ค. ์˜ค๋ฒ„๋ผ์ด๋”ฉ์„ ํ•˜๊ธฐ ์œ„ํ•ด์„  ์˜ค๋ฒ„๋ผ์ด๋“œ๋ผ๋Š” ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์ค˜์•ผํ•œ๋‹ค. ๋งŒ์•ฝ ๊ฐ™์€ ์ด๋ฆ„์˜ ์ •์˜๊ฐ€ super class์— ์—†๋Š”๋ฐ ์˜ค๋ฒ„๋ผ์ด๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ์ปดํŒŒ์ผ๋Ÿฌ ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค. ๋ฐ˜๋Œ€๋กœ, ์žฌ์ •์˜๋ฅผ ์œ„ํ•ด์„œ ์˜ค๋ฒ„๋ผ์ด๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์ด๋˜ํ•œ ์ปดํŒŒ์ผ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

โ–ท ์œ„์—์„œ ๋งŒ๋“  Vehicle class์˜ func ๋ฉ”์„œ๋“œ makeNoise ํ•จ์ˆ˜๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•ด๋ณด๊ฒ ๋‹ค.

/* ๊ธฐ์กด ํ•จ์ˆ˜
class Vehicle {
	var currentSpeed = 0.0
    var description: String {
    	return "traveling at \(currentSpeed) miles per hour"
	}
    func makeNoise() {
    }
}
*/

์œ„ ๊ธฐ์กด Vehicle ์„ super class๋กœ ํ•˜๋Š” sub class๋ฅผ ๋งŒ๋“ค์–ด๋ณด๊ฒ ๋‹ค.

class Train: Vehicle {
	override func makeNoise() {
    	print("choo choo")
	}
}

let train = Train()
train.makeNoise()
//prints "choo choo"

์ƒ์†๋ฐ›์€ makeNoise ํ•จ์ˆ˜๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•˜์—ฌ ์žฌ์ •์˜ํ•œ ๊ฐœ๋…์„ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๋งŒ์•ฝ ์ด๋ ‡๊ฒŒ ์„œ๋ธŒํด๋ž˜์Šค์—์„œ ์Šˆํผํด๋ž˜์Šค์˜ ํŠน์„ฑ์„ ์žฌ์ •์˜ ํ–ˆ์„ ๋•Œ, ์žฌ์ •์˜ ์ด์ „ ์Šˆํผํด๋ž˜์Šค์˜ ์›๋ž˜ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ? 

์ด๋Ÿด๋•Œ๋Š” super๋ผ๋Š” ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. 

class Vehicle {
    var type: String
	var currentSpeed = 0.0
	
	init(type: String){
	    self.type = type
	}
	
    func makeNoise() {
    	print("\(type) ์†Œ๋ฆฌ ๋“ค์„๋ž˜?")
    }
}

class Train: Vehicle {
    override func makeNoise() {
            super.makeNoise()
            print("choo choo")
    }
}

var train = Train(type:"๊ธฐ์ฐจ")
train.makeNoise()
/* prints 
"๊ธฐ์ฐจ ์†Œ๋ฆฌ ๋“ค์„๋ž˜?
choo choo" */

์œ„ ์ฝ”๋“œ์—์„œ ์•„๋ž˜ ์ƒ์† ๋ฐ›๋Š” ํด๋ž˜์Šค Train์„ ๋ณด๋ฉด, override ํ•จ์ˆ˜ ์•ˆ์— super.makeNoise๋ฅผ ๋จผ์ € ์ถœ๋ ฅํ•ด์ฃผ์—ˆ๋‹ค.

 

โ–ท ์ด๋ฒˆ์—๋Š” ํ”„๋กœํผํ‹ฐ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ•ด๋ณด์ž.

๋ฉ”์„œ๋“œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์Šˆํผํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ์ƒ์†๋ฐ›์€ ์ธ์Šคํ„ด์Šคํ”„๋กœํผํ‹ฐ๋„ ์žฌ์ •์˜ ๊ฐ€๋Šฅํ•˜๋‹ค. ํ”„๋กœํผํ‹ฐ๋ฅผ ์žฌ์ •์˜ํ•œ๋‹ค๋Š” ๊ฒƒ์€ ํ”„๋กœํผํ‹ฐ ์ž์ฒด๋ฅผ ์•„๋‹ˆ๊ณ , getter, setter, observer๋ฅผ ์žฌ์ •์˜ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค. ์˜ˆ์ œ๋ฅผ ์‚ดํŽด๋ณด์ž.

/* ๊ธฐ์กด ํ•จ์ˆ˜
class Vehicle {
	var currentSpeed = 0.0
    var description: String {
    	return "traveling at \(currentSpeed) miles per hour"
	}
    func makeNoise() {
    }
}
*/

class Car: Vehicle {
	var gear = 1
    override var descripton: String {
    	return super.decription + " in gear \(gear)"
	}
}

let car = Car()
car.currentSpeed = 30.0
car.gear = 2
print(car.description)
//prints "traveling at 30.0 miles per hour in gear 2"

์„œ๋ธŒ ํด๋ž˜์Šค์—์„œ ์žฌ์ •์˜ํ•˜๊ธฐ ์œ„ํ•œ ํ”„๋กœํผํ‹ฐ๋Š”, ์Šˆํผํด๋ž˜์Šค ํ”„๋กœํผํ‹ฐ์˜ ์ด๋ฆ„๊ณผ ํƒ€์ž…์ด ์ผ์น˜ํ•ด์•ผํ•œ๋‹ค.

 

+) final ์ด๋ž€?

๊ธฐ๋ณธํด๋ž˜์Šค์— ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ• ๋•Œ final ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์žฌ์ •์˜ํ•  ์ˆ˜ ์—†๋Š” ํ”„๋กœํผํ‹ฐ๋กœ ์ƒ์„ฑ๋œ๋‹ค.

class Vehicle {
	final var currentSpeed = 0.0
    var description: String {
    	return "traveling at \(currentSpeed) miles per hour"
	}
    func makeNoise() {
    }
}

์ด๋ ‡๊ฒŒ override ํ•  ์ˆ˜ ์—†๋‹ค๋Š” ์ปดํŒŒ์ผ ์—๋Ÿฌ๊ฐ€ ๋œจ๊ฒŒ ๋œ๋‹ค. 

๋ฐ˜์‘ํ˜•