0x00 Lesson
Structs can have functions inside them, and those functions can use the properties of the struct as they need to. Functions inside structs are called methods
, but they still use the same func
keyword.
We can demonstrate this with a City struct. This will have a population property that stores how many people are in the city, plus a collectTaxes() method that returns the population count multiplied by 1000. Because the method belongs to City it can read the current city’s population property.
struct City {
var population: Int
func collectTaxes() -> Int {
return population * 1000
}
}
That method belongs to the struct, so we call it on instances of the struct like this:
let london = City(population: 9_000_000)
london.collectTaxes()
0x01 我的小作品
欢迎体验我的作品之一:小五笔 86 版
五笔学习好帮手
App Store
搜索即可~
原文地址:https://blog.csdn.net/xjh093/article/details/128231535
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_47884.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!