// Class attribute definition
class Student: NSObject {
// Define attributes
// Define storage attributes
var age: Int = 0
var name: String?
var mathScore: Double = 0.0
var chineseScore: Double = 0.0
// Define a method that can return the average score (note: swift is not recommended to use this way, a calculated attribute should be defined)
func getAverageScore ()-> Double {
// If you use a certain property of the current object in swift, or call a method of the current object, you can use it directly without adding self
return (mathScore chineseScore) * 0.5
}
// Define the calculated attribute: the attribute whose result is calculated in another way is called the calculated attribute
// A lot of calculated attributes
var averageScore: Double {
return (mathScore chineseScore) * 0.5
}
// Define class attributes: Class attributes are attributes related to the entire class, and are accessed directly through the class name
// Generally used for more points in simple interest
static var courceCount: Int = 0
}
// Assign values to object properties
let stu = Student ()
stu.age = 10
stu.name = "wj"
stu.mathScore = 78
stu.chineseScore = 70
print (stu.age)
if let name = stu.name {
print (name)
}
let averageScore = (stu.mathScore stu.chineseScore) * 0.5
let ave = stu.averageScore
// Class attributes
// Assign values to class attributes
Student.courceCount = 2
swift-storage properties, computed properties, class properties