In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the "kotlin in the setter and getter attributes how to use" related knowledge, editor through the actual case to show you the process of operation, the method of operation is simple and fast, practical, I hope that this "how to use the setter and getter attributes in kotlin" article can help you solve the problem.
1. In Kotlin, getter and setter are optional, and if you don't create them in your code, it will be generated automatically by default.
Class Account {
Var name: String = ""
Var age: Int = 0
Var balance: Double = 0.0
}
Equivalent to
Class Account {
Var name: String = ""
Var age: Int = 0
Var balance: Double = 0.0
/ / this set and get method is not recommended to write it manually in the code.
Set (vaule) {
Field = value
}
Get () = field
}
Note:
The 1.field keyword represents the current domain.
The 2.var keyword is declared as a mutable attribute.
The 3.val keyword is declared as a read-only property.
Use:
Val mAccount = Account ()
/ / assignment
MAccount.name = "Qinchuan Young General"
MAccount.age = 18
MAccount.balance = 100.0
/ / printout
Println ("${mAccount.name}")
Println ("${mAccount.age}")
Println ("${mAccount.balance}")
Output:
... / com.sample.app I/System.out: Qin Chuan young general
... / com.sample.app I/System.out: 18
... / com.sample.app I/System.out: 100.0
two。 If you add modifiers to the properties of an object, you can write the setter and getter methods manually
Class Account {
Private var name: String = ""
Private var age: Int = 0
Private var balance: Double = 0.0
Fun setName (name: String) {
This.name = name
}
Fun getName (): String {
Return this.name
}
Fun setAge (age: Int) {
This.age = age
}
Fun getAge (): Int {
Return this.age
}
Fun setBalance (balance: Double) {
This.balance = balance
}
Fun getBalance (): Double {
Return this.balance
}
}
Use:
Val mAccount = Account ()
/ / assignment
MAccount.setName ("Qinchuan Young General")
MAccount.setAge (18)
MAccount.setBalance (100.0)
/ / printout
Println ("${mAccount.getName ()}")
Println ("${mAccount.getAge ()}")
Println ("${mAccount.getBalance ()}")
Output:
... / com.sample.app I/System.out: Qin Chuan young general
... / com.sample.app I/System.out: 18
... / com.sample.app I/System.out: 100.0
If there is no modifier, the first scheme is recommended, and the properties in the kotlin object are decorated by public by default.
This is the end of the introduction to "how to use the setter and getter properties in kotlin". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.