Kotlin Getting Started (iii)--variable and question mark _kotlin

Source: Internet
Author: User
Reference

Website links. Question mark?

When defining a variable, you can add a question mark after the type, indicating that the variable is nullable, and that the variable is not nullable. As follows:

var s:string? = null
var s2:string = "XXX"//If the variable is assigned null, compilation does not pass
The setter method that intercepts two variables from its corresponding Java file (which is decompile from the compiled class file) is as follows:

It can be seen that when setting the value for the variable s, the value is directly assigned, while for the Variable S2 assignment, the first is a non-null judgment, which guarantees that S2 cannot be null.

For variables that can be null, when using the variable, you must use the variable name +? (as above S?) To indicate that if the variable is null, the method that the variable is called is not executed. Such as:

	s = "yyy"///assignment does not need to use the S
	-form var l= s? length//if S is null, the length method is not executed
When S is null, the length method is not executed, thus avoiding frequent null-pointer exceptions in Java. var and Val all member variables (including defined in the class and directly defined in the KT file) must be initialized when defined, and local variables (defined within the method) can be uninitialized. Such as:
var s:string? = null
var s2:string = ""

Fun Main (args:array<string>) {
	var t:test = Test ()
	var m:int//can not initialize c4/>}
class test{
	var test:string = ""
}
S,s2,test three variables in the above code must be initialized, but not m is not required.

var: defines a variable variable.

Val: Defining constants, the defined variables are final types. such as Kotlin source code is:

var a:int = 2
val B = 1

Decompile its class file as follows, from which B is the final type, and A is mutable:


Getter and setter

The complete definition of a variable is as follows:

var <propertyname>[: <propertytype>] [= <property_initializer>]
    [<getter>]
    [< Setter>]

Variables of type var can define setter,val to represent constants and cannot define setter methods. <> is required, [] is optional.

For variable types (propertyname), if it can be inferred from the initial value or inferred from the return value of the Getter method, you can omit not to write, otherwise you must write.

Defines the getter or setter through get () or set (), and uses field to represent the current variable. For the default getter and setter implementations, you can not define them unless you want to do more in the getter or setter. As follows:

Fun Main (args:array<string>) {
	val d = Demo ()
	d.test= "This was from test"//Will call the Setter method
	println (d.test //Will invoke the Getter method
}
class demo{
	var test= "haha" Get
		(): string{//specifies, by get (), that the Getter println is currently defined
			(" Call getter "] return
			field//field instead of the variable, cannot be written directly as variable name test
		}
		set (value:string) {//setter specified currently defined by setters , the type of value can omit
			println ("Call setter")
			field = value//also represent variables through field, not written as variable name Test
		}

For a set method, its parameter type must be the type of the variable. For example, the above test is of type string, then the parameter of set () must be of type string and cannot be set to an int or other type.

For getter, if it is a single statement function, it can also be abbreviated. As follows:

Class demo{
	var test= "haha"
		get () = Field//getter returns only the values of the fields, so
		it is not possible to abbreviate set (value) {//setter because it is set (value = Field = value Compilation error
			field = value
		}
}

Although the variable name of the setter method above is value, it is not fixed to death and can be changed at will.

You can also modify the getter or setter by using private keywords to change the visibility of these methods, as follows:

Fun Main (args:array<string>) {
	var t = Demo ()
	t.test = "Test Private"//This sentence compiles and does not pass because its setter method is private
	println (t.test)
}


Class demo{
	var test= "haha" get
		() = field
		Private set//define setter method as private type
}
Field

In getter and setter, field is used to represent the current variable, and the variable name cannot be used directly. As follows:

Class demo{
	var test= "haha"
		get () = Field//This cannot be returned directly to test, otherwise it will be invoked indefinitely until the report is Java.lang.StackOverflowError.
}
Note that the field identifier can only be used in getter and setter and cannot be used elsewhere.

In addition, the Field field is available only if you have a default implementation that is used in the Field field or in Getter,setter. There is no other time for this field.








































Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.