::public/Swift

protocol

해맑은욱 2022. 11. 16. 14:40
//protocol: 특정 역할을 하기 위한 메소드, 프로퍼티, 기타 요구사항 등의 정의..

protocol FirstProtocol {
	var name: String { get set }	// 읽기, 쓰기가 동시에 가능한 프로퍼티는 { get set }을 써주어야 함
	var age: Int { get }			// 읽기만 가능한 프로퍼티
}

protocol SecondProtocol {
	static var someTypeProperty: Int { get set }	// 타입프로퍼티를 요구하려면 static을 선언해줘야 함
	
	func someTypeMethod()
}

// 여러가지 프로토콜을 동시에 채택 가능. 프로토콜에서 요구하는 프로퍼티, 메소드를 정의해주어야함
(struct/class) ProtocolClass: FirstProtocol, SecondProtocol {
	var name: String
	var age: Int
	
	func someTypeMethod() {
	
	}
}