In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what are the basic knowledge points of Swift2.0 objects". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Classes and instances (objects)
Class "is a reference type", not "value type"
Define a class using the class keyword
Class People {
Var name:String = ""
Var age = 0
}
People is the class name
{} contains the body of the class, including properties and behaviors
Naming convention: class names with uppercase initials
You can create instance variables or instance constants through classes
Var p1:People = People ()
Let p2 = People ()
People () indicates that the constructor of the People class is called
The instance object is passed. Operator to access properties and behaviors
Attribute
You can have default values or no default values (described later)
It can be a variable or a constant. A constant must have a default value.
The type of the property can also be an optional type
Properties can be divided into two main categories:
Storage attribute: occupies storage space
Behavior attribute: no storage space is occupied, and the method is actually called when accessing the property
Static storage properties:
Use the static declaration to belong to the entire type rather than an instance object
Access static storage properties through class name
Lazy storage properties: (storage properties)
This attribute will only exist after a certain period of time or after a certain condition has set out.
Before that, the property was just a placeholder with no storage space.
The lazy property implements a function similar to lazy loading (when the value of the property requires complex or massive computation)
Keyword: lazy, which must be a variable and must have an initial value, such as:
Class People {
Var name:String = ""
Lazy var age = 0
Func printAge () {
Print ("age is\ (age)")
}
}
Var p1:People = People ()
P1.printAge ()
After creating the p1 instance, you can view the age.storage=nil through the debugger
After calling the printAge () method, you can see the age.storage=0 through the debugger because you have accessed the age property
The initial value of the inert attribute is usually set to the return value or object of the function to achieve the function of lazy loading.
Behavior attributes:
Although it is an attribute, there is no actual storage space. Value accessor get and assignment accessor set are provided.
The behavior attribute must be a variable and cannot have an initial value
The syntax of get:
Var behavior attribute: type {
Get {
Return values that match the type
}
}
It can be simplified as follows:
Var behavior attribute: type {
Return values that match the type
}
The syntax of set:
Var behavior attribute: type {
Set (newValue) {
/ / Operation
}
}
It can be simplified as follows:
Var behavior attribute: type {
The parameter name omitted by set {/ / Operation} / / defaults to newValue.
}
Such as:
Class People {
Var mindDesc:String = ""
Var dream:String = ""
Var reality:String = ""
Var isHappy:Bool {
Get {
If (! dream.isEmpty & & dream = = reality) | | mindDesc = = "Happy" {
Return true
}
Else {
Return false
}
}
Set (newValue) {
If (newValue) {
MindDesc = "Happy"
}
Else {
MindDesc = "No Happy"
}
}
}
}
Var p1 = People ()
P1.isHappy = true
/ / p1.dream = "medium 5 million"
/ / p1.reality = "didn't win the lottery"
Print (p1.mindDesc)
Print (p1.isHappy)
If no set assignment accessor is provided, the property is read-only, such as:
Var isHappy:Bool {
If (! dream.isEmpty & & dream = = reality) | | mindDesc = = "Happy" {
Return true
}
Else {
Return false
}
}
Class behavior properties:
Class behavior attribute is a special behavior attribute, which belongs to the whole type, not to an instance object.
Decorated with class, accessed through the class name
Also known as static behavior attributes (static is generally used for structures, enumerations, and class for class types)
Syntax: class var attribute name: type {get/set {}}
Such as:
Class People {
Class var maxHeight:Float {
Return 2.40
}
}
Print (People.maxHeight)
The observer for the property:
The period used to detect changes in attributes, divided into two observation periods: willSet and didSet
Syntax: willSet (newValue) {} didSet (oldValue) / / parameters can be omitted
Such as:
Class People {
Var salary:Int = 200 {
WillSet {
Print ("salary adjustment!")
}
DidSet {
If salary > oldValue {
Print ("pay rise!")
}
Else if salary
< oldValue { print("降工资了!") } } } } var p1:People = People() p1.salary = 300 p1.salary = 150 属性观察器实现了类似KVO模式的功能 属性观察器同样适用于全局变量和局部变量,其中全局变量可理解为省略了lazy的惰性行为变量 方法 方法是与类型相关联的函数,是类应具有的行为动作,与C++/Java这些语言不同的是:在结构体和枚举中都可以定义方法 对象方法: 实例方法通过实例对象通过.运算符调用 如: class People { var salary:Int = 200 func receiveSalary(date:String)->Int {
Print ("pay (date) salary!")
Return salary
}
}
Var p1:People = People ()
Var s = p1.receiveSalary ("February 2016")
Print ("salary is\ (s)")
The property can be accessed directly inside the instance method, because the scope of the property is inside the class
The instance method can be called directly inside the instance method, because the scope of the instance method is inside the class
Note: methods are also functions, and the rules of functions are supported.
Class method:
Similar to a type property, it does not belong to an instance object, but to the entire class, calling through the class name
Syntax: class func method name (parameter list)-> return value {}
Also known as "static methods" (static for structures, enumerations, and class for class types)
Such as:
Class People {
Class var desc:String {
Return "Human"
}
Class func printDesc () {
Print ("We are\ (desc)")
}
}
People.printDesc ()
It should be noted that within class methods, only class behavior properties, static properties, and class methods can be accessed.
The self keyword in the method
In the object method, self represents the current instance object
In class methods, self represents the current class
Subscript script method:
Is a special object method provided by Swift. The object calls the subscript method through the subscript s operator
Syntax: subscript (parameter list)-> return value {get/set accessor}
Such as:
Class People {
Var names: [Int:String] = [:]
Subscript (index:Int)-> String? {
Get {
Return names [index]
}
Set {
Names [index] = newValue
}
}
}
Var p:People = People ()
P [001] = "Zhang San"
P [007] = "Li Si"
Print (p [001]!)
Print (p [007]!)
The parameters of the subscript method can be multiple or even variable, cannot be modified with inout, and the default parameter value cannot be set.
Subscript methods can be overloaded or read-only, such as:
Class People {
Var names: [Int:String] = [:]
Subscript (index:Int)-> String? {
Get {
Return names [index]
}
Set {
Names [index] = newValue
}
}
Subscript (index:Int, appendString:String)-> String? {
Get {
Let s:String? = names [index]! + appendString
Return s
}
}
}
Var p:People = People ()
P [001] = "Zhang San"
P [007] = "Li Si"
Print (p [001]!)
Print (p [007, "Hello"]!
Access to swift
Three access modifiers: private internal public
Access control is not class-based, but file-based, that is, decorating identifiers can be accessed in which source files
Modifiable identifiers: class name, structure name, enumeration name, method name, property name, etc.
Private: only accessible in the current source file
Internal: can only be accessed in the same module as the current source file (default permissions, you don't have to write)
Public: accessible from any source file
This is the end of the content of "what are the basic knowledge points of Swift2.0 objects". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.