프로토콜에서 사용되는 함수나 프로퍼티의 디폴트값을 넣고싶은경우 extension 을 사용하면된다
protocol AppleDevice {
func findMyDevice()
func checkSerialNumber()
var modelNumber:String {
get set
}
}
extension AppleDevice {
func findMyDevice() {
print("Find My AppleDevice...")
}
func checkSerialNumber() {
print("You Can Check your SerialNumber Froe Box")
}
var modelNumber:String {
get { return "Not Yet"}
set { modelNumber = newValue}
}
}
class iPhone: AppleDevice {
var modelNumber: String {
return "MNP-23"
}
var master:AppleCompany?
init(master:AppleCompany) {
master.delegate = self
}
var serialNumber: String {
var SerialNumber = "iPhone"
for _ in 0...15 {
SerialNumber += String(Int.random(in: 0...9))
}
return SerialNumber
}
func findMyDevice() {
print("Find My Iphone...")
}
func checkSerialNumber() {
print(self.serialNumber)
}
}
class Ipad: AppleDevice {
var master:AppleCompany?
var modelNumber: String {
return "MAT-19"
}
init(master:AppleCompany) {
master.delegate = self
}
var serialNumber: String {
var SerialNumber = "iPad"
for _ in 0...15 {
SerialNumber += String(Int.random(in: 0...9))
}
return SerialNumber
}
func findMyDevice() {
print("Find My Ipad...")
}
func checkSerialNumber() {
print(self.serialNumber)
}
}
class iMac: AppleDevice {
var modelNumber: String {
return "MO3-38"
}
var master:AppleCompany?
var serialNumber: String {
var SerialNumber = "iMac"
for _ in 0...15 {
SerialNumber += String(Int.random(in: 0...9))
}
return SerialNumber
}
init(master:AppleCompany) {
master.delegate = self
}
func findMyDevice() {
print("Find My iMac...")
}
func checkSerialNumber() {
print(self.serialNumber)
}
}
class AppleCompany {
var delegate:AppleDevice?
func RecallDevice() {
print("Recall device")
}
func forcedFindMyDevice() {
delegate?.findMyDevice()
}
}
class newDevice:AppleDevice {
var master:AppleCompany?
var serialNumber: String {
var SerialNumber = "new"
for _ in 0...15 {
SerialNumber += String(Int.random(in: 0...9))
}
return SerialNumber
}
init(master:AppleCompany) {
master.delegate = self
}
}
let steave = AppleCompany()
let iPhone13 = iPhone(master: steave)
print(iPhone13.modelNumber)
iPhone13.checkSerialNumber()
steave.forcedFindMyDevice()
let iPad3 = Ipad(master: steave)
print(iPad3.modelNumber)
iPad3.checkSerialNumber()
steave.forcedFindMyDevice()
let iMac3 = iMac(master:steave)
print(iMac3.modelNumber)
iMac3.checkSerialNumber()
steave.forcedFindMyDevice()
let newDevice3 = newDevice(master: steave)
newDevice3.findMyDevice()
newDevice3.checkSerialNumber()
print(newDevice3.modelNumber)
//출력값
MNP-23
iPhone2997174408378184
Find My Iphone...
MAT-19
iPad1105544854982974
Find My Ipad...
MO3-38
iMac2108360658256554
Find My iMac...
Find My AppleDevice...
You Can Check your SerialNumber Froe Box
Not Yet
newDevice class 안에는 따로 프로토콜 함수나 프로퍼티를 정의하지않아도 extension에서 정의한 값이 호출된다...