Swift class
Swift class is a general and flexible construct for building code.
We can define properties (constants, variables) and methods for classes.
Different from other programming languages, Swift does not require you to create independent interface and implementation files for custom classes. All you have to do is define a class in a single file, and the system automatically generates external interfaces to other code.
Comparison of classes and structures
Classes and structures in Swift have a lot in common. The common points are:
Define properties to store values
Define methods to provide functions
Define ancillary scripts for accessing values
Define constructors for generating initialization values
Extend to increase the functionality of the default implementation
Conform to the protocol to provide standard functions for a certain class
Compared with structures, classes have the following additional functions:
Inheritance allows one class to inherit characteristics of another class
Type conversion allows the type of a class instance to be inspected and interpreted at runtime
Destructor allows a class instance to release any allocated resources
Reference counting allows multiple references to a class
Grammar:
Class classname { Definition 1 Definition 2 …… Definition N }
Class definition
class student{ var studname: String var mark: Int var mark2: Int }
Instantiation class:
let studrecord = student()
Instance
import Cocoa class MarksStruct { var mark: Int init(mark: Int) { self.mark = mark } } class studentMarks { var mark = 300 } let marks = studentMarks() print("成绩为 \(marks.mark)")
The output result of the execution of the above program is:
成绩为 300
Accessing class attributes as reference types
The attributes of the class can be accessed through .. The format is: Instanced class name.Attribute name:
import Cocoa class MarksStruct { var mark: Int init(mark: Int) { self.mark = mark } } class studentMarks { var mark1 = 300 var mark2 = 400 var mark3 = 900 } let marks = studentMarks() print("Mark1 is \(marks.mark1)") print("Mark2 is \(marks.mark2)") print("Mark3 is \(marks.mark3)")
The execution output of the above program is:
Mark1 is 300 Mark2 is 400 Mark3 is 900
Identity operator
Because classes are reference types, there may be multiple constants and variables referencing a certain class instance in the background at the same time.
In order to determine whether two constants or variables refer to the same class instance, Swift has two built-in identity operators:
Identity operator | Identity operator |
---|---|
Operation The symbol is: !== | |
If the two constants or variables refer to different class instances, then Return true |
import Cocoa
class SampleClass: Equatable {
let myProperty: String
init(s: String) {
myProperty = s
}
}
func ==(lhs: SampleClass, rhs: SampleClass) -> Bool {
return lhs.myProperty == rhs.myProperty
}
let spClass1 = SampleClass(s: "Hello")
let spClass2 = SampleClass(s: "Hello")
if spClass1 === spClass2 {// false
print("引用相同的类实例 \(spClass1)")
}
if spClass1 !== spClass2 {// true
print("引用不相同的类实例 \(spClass2)")
}
The above program execution output result is: 引用不相同的类实例 SampleClass