- iOS Programming Cookbook
- Hossam Ghareeb
- 214字
- 2021-07-09 18:29:40
How to do it...
- Create a new playground file in Xcode called Protocols as usual.
- Complete the following example using the following protocol:
protocol VehicleProtocol{ // properties var name: String {set get} // settable and gettable var canFly: Bool {get} // gettable only (readonly) // instance methods func numberOfWheels() ->Int func move() func stop() // class method staticfuncpopularBrands() -> [String] } class Bicycle: VehicleProtocol{ var name: String var canFly: Bool{ return false } init(name: String){ self.name = name } func numberOfWheels() -> Int { return 2 } func move() { // move logic goes here } func stop() { // stop logic goes here } static func popularBrands() -> [String] { return ["Giant", "GT", "Marin", "Trek", "Merida", "Specialized"] } } class Car: VehicleProtocol{ var name: String var canFly: Bool{ return false } init(name: String){ self.name = name } funcnumberOfWheels() ->Int { return 4 } func move() { // move logic goes here } func stop() { // stop logic goes here } static func popularBrands() -> [String] { return ["Audi", "BMW", "Honda", "Dodge", "Lamborghini", "Lexus"] } } let bicycle1 = Bicycle(name: "Merida 204") bicycle1.numberOfWheels() // 2 let car1 = Car(name: "Honda Civic") car1.canFly // false Bicycle.popularBrands() // Class function // ["Giant", "GT", "Marin", "Trek", "Merida", "Specialized"] Car.popularBrands() // ["Audi", "BMW", "Honda", "Dodge", "Lamborghini", "Lexus"]
推薦閱讀
- Windows Server 2019 Cookbook
- Mastering vRealize Operations Manager(Second Edition)
- Linux系統(tǒng)文件安全實戰(zhàn)全攻略
- Google系統(tǒng)架構(gòu)解密:構(gòu)建安全可靠的系統(tǒng)
- 蘋果電腦玩全攻略 OS X 10.8 Mountain Lion
- 操作系統(tǒng)基礎(chǔ)與實踐:基于openEuler平臺
- Implementing Azure DevOps Solutions
- Java EE 8 Design Patterns and Best Practices
- AutoCAD 2014中文版從入門到精通
- Kali Linux 2018:Windows Penetration Testing
- 從實踐中學習Kali Linux無線網(wǎng)絡(luò)滲透測試
- 云原生落地:產(chǎn)品、架構(gòu)與商業(yè)模式
- Cassandra 3.x High Availability(Second Edition)
- Windows 7實戰(zhàn)從入門到精通
- Advanced Infrastructure Penetration Testing