Kotlin Programming related knowledge points : Kotlin programming using IntelliJ IED and understanding Source files (. kt) Kotlin programming and other properties Kotlin programming methods such as using Kotlin programming Kotlin programming of the parent class and the inherited parent class Kotlin programming interface and implementation interface Kotlin programming of the associated object, abstract class, sealed class Kotlin programming nested class, Inner class, Anonymous inner class Kotlin programming object expression and declaration Kotlin programming extension method Kotlin programming extended attributes and extended companion object generics
In Kotlin programming, generics are supported
Package com.xingen.kotlin.day2017528 Fun
Main (args:array<string>) {
var instance=test<string> ( "Define a generic class")
instance.test ()
//When you can infer a type in a generic, for example: from a constructor parameter, you can omit the specified parameter type.
Test (1). Test ()
}
/**
* Defines a generic class, and generic properties */
class test<t> (var t:t) {Fun Test () {
println (T)
}
}
Output Result:
Defining a generic class
1
When you can infer a type in a generic, such as a parameter from a constructor, you can omit the point of attention for the generic type of the specified parameter type:
The most tricky part of a type system in Java programming is a wildcard type. However, in Kotlin programming, there is no wildcard character, which is replaced by a declaration change and a type projection.
The role of wildcards: Use the bounds wildcard to increase the flexibility of the API.
A common problem in Java programming:
List<string> STRs = new arraylist<string> ();
list<object> OBJS = STRs; // !!! This will cause a problem in Java that is forbidden to do so in
objs.add (1);//Add the data of type integer to the list of type string
s = strs.get (0);//!!! ClassCastException exception: integer type cannot be converted to string
In Kotlin programming:
Once the type is declared, no other type data can be added, as shown in the following figure.