当前位置:网站首页>Use of swift protocol
Use of swift protocol
2022-04-22 20:18:00 【Autumn trifles】
//
// Protocols.swift
// SwiftCode
//
// Created by Alisa on 2022/4/14.
// Copyright 2022 Alisa. All rights reserved.
//
import Foundation
/*
agreement : In grammar , Some properties and methods can be defined in the protocol , These properties and methods are just declarations , These properties and methods cannot be implemented in the protocol . The protocol is not a data type , No,
Construction method , No instantiation is required
swift in , Agreement to use protocol Keyword creation , Which can declare properties and methods , The attribute can be a calculation attribute in the concrete implementation , It can also be a storage attribute
Combination of protocol and extension :
Extensions can add new functions to data types , Through extension, you can also provide a default implementation for the attribute methods agreed in the protocol .
Developers can provide an extension to a protocol , Provide a set of default implementation for its agreed attribute methods in the extension , In this way, all data types that comply with this protocol get the default implementation in the extension
*/
protocol BookMarkProtocol{
/* ** Defining attributes */
// read-only
var bookColor:String { get }
// Can read but write
var bookPages:Int {get set}
// Static attribute
static var bookSize:(Int, Int){get set}
/* ** Define methods */
// Example method
func markLiteraryBooks()->Void
// Static methods
static func cleanBookMark()->Void
}
class Bookmark{
var bookName:String
var Bentence:String
var color:String
var pages:Int
static var size:(Int, Int) = (20, 20)
// Protocol attribute
// read-only
var bookColor:String{
get{
return self.color
}
}
// Can read but write
var bookPages:Int{
get{
print(" Instance calculation properties of the protocol , Read book Of pages:\(self.pages)")
return self.pages
}
set{
print(" Instance calculation properties of the protocol , Set up book Of pages:\(newValue)")
self.pages = newValue
}
}
// Static attribute
static var bookSize:(Int, Int){
get{
print(" Static computing properties of the protocol , Read book Of size:\(self.size)")
return self.size
}
set{
print(" Static computing properties of the protocol , Set up book Of size:\(newValue)")
self.size = newValue
}
}
// Protocol method
// Example method
func markLiteraryBooks(){
print(" Put in literature books book mark")
}
// Static methods
static func cleanBookMark(){
print(" Clean up the current book mark")
}
init() {
self.bookName = ""
self.Bentence = ""
// Protocol added properties
self.color = ""
self.pages = 0
}
deinit{
print("Bookmark deinit")
}
}
// The combination of protocol and extension
@objc protocol ClassProcotol:AnyObject{
// This protocol method can choose whether to implement , That is, classes that comply with the protocol can implement , It can also not be realized
@objc optional func log()
}
// by ClassProtocol The method in provides a default implementation
extension ClassProcotol{
func log(){
print("show this log")
}
}
class AirConditioner:ClassProcotol{
// When accepting this Agreement , There was a warning :Non-'@objc' method 'log()' does not satisfy optional requirement of '@objc' protocol 'ClassProcotol'
}
class Protocols{
// Use instance properties and instance methods in the protocol
func useInstanceProtocols(){
let mark = Bookmark()
// Set the calculation properties in the protocol
mark.bookPages = 22
// Set properties
mark.color = "blue"
// Read the calculation properties in the protocol
let color = mark.bookColor
let pages = mark.bookPages
print(" The color of the current book :\(color), The number of pages in the current book :\(pages)")
// Instance method in protocol
mark.markLiteraryBooks()
/* Print information :
Instance calculation properties of the protocol , Set up book Of pages:22
Instance calculation properties of the protocol , Read book Of pages:22
The color of the current book :blue, The number of pages in the current book :22
Put in literature books book mark
Bookmark deinit
*/
}
// Use static properties and static methods in the protocol
func useStaticProtocols(){
// Static properties in the protocol
let size = Bookmark.bookSize
print("size Standard size of :\(size)")
// Static methods in the protocol
Bookmark.cleanBookMark()
/* Print information
size Standard size of :(20, 20)
Clean up the current book mark
*/
}
// The combination of protocol and extension
func useExtensionProtocols(){
let acd = AirConditioner()
acd.log() //show this log
}
}
版权声明
本文为[Autumn trifles]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204222017444129.html
边栏推荐
- Where should 2021 fresh graduates go?
- 对话木瓜移动创始人沈思 l 从硅谷到北京
- Better implementation ideas based on MySQL in some scenarios (continuous update)
- The new pytorch library by the author of "lottery hypothesis" is popular
- Screen adaptation of Android interview questions + Aidl
- Review of SSM framework
- MySQL query data within a radius length of latitude and longitude
- Format for creating Zimbra LDAP users using LDAP clients
- Self built CA center to issue certificates for different applications of the company
- Academician Mei Hong: how to construct artificial swarm intelligence
猜你喜欢

2022年土建施工员题库精准小题库建设厅施工员

Activity Result API 使用与源码分析

<山东大学项目实训>辐射预计算渲染及后处理降噪系统研究(一)

掌握这些引用参考文献的小Tips,助您论文写作事半功倍~
![[basic knowledge of automated testing] basic concepts and common frameworks of automated testing](/img/88/dd4884bf12474ca1870147462c9e07.png)
[basic knowledge of automated testing] basic concepts and common frameworks of automated testing

模块四作业

Format for creating Zimbra LDAP users using LDAP clients

Filebeat

看大网站如何玩转网站加速(CDN)的秘密

资料员考试题型有哪些怎么备考建设厅资料员考试
随机推荐
微服务笔记合集
金仓数据库KingbaseES的安全特性
工作光靠努力是不够的!还有这个...
Comparison and principle summary of golang local cache selection
2022-01-12 wechat applet debugging
See how big websites play the secret of website acceleration (CDN)
Write a gateway service, understand more thoroughly!
Timestamp conversion
The WiFi next door, I break every second
域名解析过程
Filebeat
Implementation of calico official website network topology: Based on ENSP and vmvare
Collection of microservice notes
UML(统一建模语言)知识学习
Perfect forwarding implementation mechanism
ZTNA (Zero Trust Network Access)
How can the "vanguard" and "Internet Aegis" effectively deploy defense for cities?
木瓜移动课堂:产品更新,Facebook新增素材预审功能,力控广告违规~
Boot implementation of IAP
The most complete interpretation of redis