当前位置:网站首页>[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 .

原网站

版权声明
本文为[Programmer Xiao He SS]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/205/202207240006243364.html