Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to parse Kotlin attributes

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article to share with you is about how to parse Kotlin attributes, Xiaobian think quite practical, so share to everyone to learn, I hope you can read this article after some harvest, not much to say, follow Xiaobian to see it.

There can be attributes in a Kotlin class. Properties can be declared mutable with the var keyword, otherwise use the val read-only keyword

attribute

1. attribute definition

Declare mutable attributes using the var keyword, attributes contain default getter and setter methods

class PropertyAndField { var propertyA: String = "" var propertyB: String = ""}

If the type has a specific value, its type can be omitted.

var name = "zhangsan"

Use val keyword to declare read-only property. Variable declared by val does not allow setter method. By default, getter method is available.

class Address { //default getters and setters var name: String = "zhangsan" //default only getters method val readonly = "readonly"}

use the property

fun main(args: Array) { var address = Address() //access property, automatically call its getter method address.name address.readonly}

2. Custom getters and setters

class Address2 { //custom setter method setter property can not be initialized var name: String get() = this.name set(value) { this.name = value }}

Omit the type of attribute. Can be omitted when getter methods can infer the type of an attribute

val no get() = 23

There can only be getter methods for val declared properties

val readonly: String get() = "readonly" //set(value){this.readonly = value} Error. val attribute cannot have setter

Change visibility of property accessors

class Address3 { var name: String = "lisi" private set //change visibility of default setter method var no: Int = 22 private set(no){this.no = no} //Change visibility of custom setter methods}

compile-time constant

Properties of known type and value can be marked as compile-time constants. There are several requirements

At the top or a member of an object

Type String or primitive type

No custom getters

Compiler constants are marked with the const modifier

const val CONST_NO = "success"

delayed initialization attribute

When a property is declared non-null, it must be initialized in the constructor. But this is inconvenient, for example properties can be initialized by dependency injection or using the setup annotation for unit tests. You still want to avoid null checking when referencing the attribute in the class body. this

Properties can be marked with the lateinit modifier

class Order { lateinit var address: Address} The above is how to parse the Kotlin attribute, Xiaobian believes that some knowledge points may be what we see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report