前端之家收集整理的这篇文章主要介绍了
Swift基础篇——结构体,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
结构体
import Foundation
struct Rect {
var width:Double = 0.0
var height:Double = 0.0
}
var r = Rect()
print("width = \(r.width) height = \(r.height)")
r.width = 100
r.height = 99
print("width = \(r.width) height = \(r.height)")
struct Rect2 {
var width:Double
var height:Double = // 逐一构造器
var r1 = Rect2(width: 10.0,height: 10.0);
struct Rect3 {
var width:Double
var height:Double = 0.0
func getWidth() -> Double{
return width
}
}
var r2 = Rect3(width: 20.0)
print(r2.getWidth())
var r3 = Rect3(width: 30.0,140)">// 取得r2这个对象的宽度
print(r3.getWidth())
struct Rect4 {
var width:Double
var height:Double = 0.0
func show() -> Void{
print("width = \(width) height = \(height)")
}
}
var r4 = Rect4(width: 10.0)
var r5 = r4
r4.show()
r5.show()
r420.0
r4.show()
原文链接:https://www.f2er.com/swift/325658.html