当前位置:网站首页>[Android kotlin] property, getter and setter
[Android kotlin] property, getter and setter
2022-07-24 00:09:00 【Programmer Xiao He SS】
Preface
We are all Android Developer , We all started using OOP Concept in Java In the development Android Applications . But in the introduction of Kotlin After language , We all began to turn Kotlin Language , Because it is now Android Recommended language for application development , In this transformation stage , We have all noticed that there are many differences between these languages . As in the Java In the same , We declare variables private , After declaration , We create a for the same variable getter And a setter Method , These methods are made public . But when we move to Kotlin when , The whole process is simplified to just one line of code .
In this article , We will understand Kotlin Medium Property、Getter and Setter. But before you start writing an article , Smiling on your face is not because you just need to Kotlin Medium Getter and Setter Write a line of code , But because you want to learn something new today
Property
Property Variables declared inside a class but outside a method ( More accurately, member variables ).Kotlin Properties can be used “var” The keyword is declared variable , You can also use “val” Keywords are declared immutable .
The following is the syntax of attribute declaration :
var <propertyName>[: <PropertyType>] [= <property_initializer>]
[<getter>]
[<setter>]
ad locum ,Property The initializer 、getter and setter It's optional . The following is the same example :
fun main(args : Array) {
val name: String = "MindOrks"
var members: Int = 5000
members = 7000 // In can be assigned any number of times
name = "New Name" // It can not be assigned again
}
By default ,Kotlin All properties and functions in are public. But in private and protected Under the circumstances , You must explicitly add it . Besides , By default Kotlin All attributes in are final . however , If I want to declare a variable private , And then put it's getter and setter Add as public ? Let's see .
Getter and Setter
seeing the name of a thing one thinks of its function ,Getter Used to get the value of a variable , and Setter Used to set the value of any variable . Please don't applaud :) Let's give you an example . Here are Getter and Setter Of Java Code :
class Community {
//private variable
private String name;
//getter for name
public getName() {
return name;
}
//setter for name
public setName(String name) {
this.name = name;
}
}
ad locum , We have two named name and roll Private variable of . besides , We also provide Getter and Setter. If we were Kotlin Write the same code in , So the code will be :
class Community {
var name: String = "MindOrks"
}
nothing more . believe me ! This is a Kotlin in getter and setter Code for .Getter and setter It is automatically generated in the code . therefore , They are created automatically when we declare properties . Above Kotlin Code Getter and Setter The equivalent code can be written as :
class Community {
var name: String = "MindOrks"
get() = field // getter
set(value) { field = value } // setter
}
that , When you can do the same thing in one line , Why write 2-3 What about line code ?
Let's look at some more examples . In the class above , That is to say Community Class , Add two more private variables .
class Community {
//private variable
private String name;
private String startingDate;
private String desc; //description of community
//getter for desc
public getDesc() {
return name + " " + startingDate;
}
//setter for name
public setDesc(String desc) {
String descArray[] = desc.split(" ");
name = descArray[0];
startingDate = descArray[1];
}
}
In the code above ,getter Method returns a community description with spaces ,setter Used to divide the description into two parts , That is, community name and community start date . The equivalent of Kotlin The code will be :
private lateinit var name: String
private lateinit var startingDate: String
var desc: String
get() = name + " " + startingDate
set(value) {
val descArray = value.split(" ".toRegex())
name = descArray[0]
startingDate = descArray[1]
}
stay Kotlin Isn't it easy to ?
We can define variables as open To override getter or setter, Then use override keyword .
//Base class
open var money: Int = 0
get() = 10000
//Extending class
override var money: Int = 0
get()= 20000
I hope you learned something new today .
边栏推荐
- C语言之字符串函数一
- Y75. Chapter IV Prometheus factory monitoring system and practice -- Prometheus alarm setting (VI)
- Ubtun update source
- 今天在家里补觉
- QT | set part size sizehint, minimumsizehint, sizepolicy, stretch factor
- Lac automatic dialing experiment of L2TP
- pytorch中with torch.no_grad(): && model.eval()
- Super simple training of force deduction and problem brushing
- 尝试新的方法
- Realize the function of uploading and downloading files and directories similar to RZ and SZ on the native terminal
猜你喜欢

Structured streaming programming model (input table, result table, output mode...)

太空射击第08课: 改进的碰撞

Application of encryption technology

docker搭建sonarqube,mysql5.7环境

Redis cluster construction (cluster cluster mode, fragment cluster)

Super simple training of force deduction and problem brushing
![[attack and defense world web] difficulty five-star 15 point advanced question: bug](/img/24/4a7f074aac9a08130cf215f0c39b57.png)
[attack and defense world web] difficulty five-star 15 point advanced question: bug

Idea cannot be switched to Chinese
![Longest increasing subsequence variant [deep understanding of the longest increasing sequence]](/img/73/1480ec319a2860fec5667d6f2fb2ba.png)
Longest increasing subsequence variant [deep understanding of the longest increasing sequence]

Chapter 7: test architecture elements
随机推荐
权重分析——熵权法
Error handling of DGS
尝试新的方法
太空射击第08课: 改进的碰撞
Qt | 设置部件大小 sizeHint、minimumSizeHint、sizePolicy、stretch factor
String function 1 of C language
Automatic escape processing in JMeter
Splicing of.Net distribution with outlook mail format and table
473-82(40、662、31、98、189)
Windows软件:如何安装Mysql5.7并配置环境变量
作为一个程序员,有什么想对新人说的吗?
北大青鸟昌平校区:运维就业现状怎么样?技能要求高吗?
【译】Go RPC 入门:Hello World
浏览器无法访问minio
.NET下发同Outlook邮件格式以及表格的拼接
Esp8266 - at command + network transparent transmission
【Android Kotlin】Property、Getter 和 Setter
Notes on cmake compilation tool
vulnhub wpwn: 1
L2TP的LAC自动拨号实验