Swift object-oriented attributes of classes and objects, swift object-oriented
In Swift, attributes of classes and objects are divided into three types: storage attributes, calculation attributes, and class attributes.
Import Foundationclass Person {// The Initial Value var score1: Int = 20 var score2: Int = 50 // The Delayed Storage attribute must be assigned to the import Foundationclass Person {// storage attribute. If needed, lazy var dog is assigned: dog = Dog () // calculates the attribute. The initial value cannot be assigned. The get and set methods are provided. var sum: Int {get {return score1 + score2 }}// class attribute, it can only be a computing attribute and can be called using classes rather than objects // such as Person. desc class var desc: String {get {return "This is one person. "}}// constructor init () {println (" This is one person. ")} class Dog {init () {println (" This is one dog. ")} var p = Person () println (Person. desc) println (p. score1) println (p. score2) println (p. sum) println (p. dog)
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.