swift-struct

// 定义一个结构体
        struct people {
            var name: String
            var age: Int
            var gender: String
        }

// 结构体实例化
        var people1 = people(name: "zhuang", age: 18, gender: "nan")
        people1.name = "李明"
        people1.age = 20
        people1.gender = "男"
        print(people1)
        var people2 = people(name: "zhuang", age: 18, gender: "nan")
        people2.name = "王凯"
        people2.age = 23
        print(people2)

// 结构体的构造函数与类当中的构造函数相似
        struct value {
            var weith: Double = 10
            var heigth: Double = 10
// 成员方法可以访问使用成员属性
            func show() {
                print("heigth = ?\(weith), weith = \(heigth)")
            }
            func getHeigth() -> Double {
                return weith*heigth
            }
        }
        var value1: value = value()
        value1.weith = 15
        value1.heigth = 20
// 成员方法调用方法 对象.方法()
        value1.show()
        value1.getHeigth()

你可能感兴趣的:(swift-struct)