Kotlin Programming Generics

Source: Internet
Author: User
Tags constructor

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.

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.