In and out, kotlinout

Source: Internet
Author: User

In and out, kotlinout

Value | thinking | Resonance


Summary: When Using Generics in Kotlin, you will notice that in and out are introduced, which may be difficult for unfamiliar developers to understand. Formally speaking, this is a way to define inverter and collaborative changes. This article will explain how to understand and remember them.

How to remember in & out?

Out (covariant)

If your class returns generics as internal methods, you can use out:

Interface Production <out T> {

Fun produce (): T
}

It can be called production class/interface, because it is mainly a generce that specifies a generic object. Therefore, you can record it as follows:Produce = output = out.

In (inverter)

If your class uses generic objects as function parameters, you can use in:

Interface Consumer <in T> {

Fun consume (item: T)

}

It can be called consumer class/interface, because it is mainly used to consume specified generic objects. Therefore, you can record it as follows:Consume = input = in.

Invariant (unchanged)

If you use generics as function parameters and function output, you do not need in or out.

Interface ProductionConsumer <T> {

Fun produce (): T
Fun consume (item: T)

}

For example

Suppose we have a hamburger object. It is a fast food, of course, a kind of food.

Open class Foodopen class FastFood: Food () class Burger: FastFood ()

1. Hamburg provider

Designed and provided based on the classes and interfaces defined aboveFood, fastfoodAndBurgerClass:

Class FoodStore: Production <Food> {
Override fun produce (): Food {
Println ("Produce food ")

Return Food ()
}
} Class FastFoodStore: Production <FastFood> {
Override fun produce (): FastFood {
Println ("Produce food ")

Return FastFood ()
}
} Class InOutBurger: Production <Burger> {
Override fun produce (): Burger {
Println ("Produce burger ")

Return Burger ()
}
}

Now, we can assign values as follows:

Val production1: Production <Food> = FoodStore ()
Val production2: Production <Food> = FastFoodStore ()
Val production3: Production <Food> = InOutBurger ()

Obviously, the hamburger store is a fast food store, and of course a food store.

Therefore, for out generics, we can assign values to the objects that use the subclass generics.

If the subclass-Burger generic type is used in turn like below, an error will occur, because the fast food and food stores not only provide the hamburger ).

Val production1: Production <Burger> = FoodStore () // Error
Val production2: Production <Burger> = FastFoodStore () // Error
Val production3: Production <Burger> = InOutBurger ()

2. Hamburger consumer

Let's define the hamburger consumer class based on the above class and interface:

Class Everybody: Consumer <Food> {
Override fun consume (item: Food ){
Println ("Eat food ")
}
} Class ModernPeople: Consumer <FastFood> {
Override fun consume (item: FastFood ){
Println ("Eat fast food ")
}
} Class American: Consumer <Burger> {
Override fun consume (item: Burger ){
Println ("Eat burger ")
}
}

Now, we canEverybody, ModernPeopleAnd American are both specified to the hamburger Consumer (Consumer <Burger> ):

Val consumer1: Consumer <Burger> = Everybody ()
Val consumer2: Consumer <Burger> = ModernPeople ()
Val consumer3: Consumer <Burger> = American ()

Obviously, the American hamburger consumers are both modern and human.

Therefore, for in generics, we can assign a value to an object that uses the parent generic type to a subclass generic type.

Similarly, if the parent class-Food generic type is used here, an error is returned:

Val consumer1: Consumer <Food> = Everybody ()
Val consumer2: Consumer <Food> = ModernPeople () // Error
Val consumer3: Consumer <Food> = American () // Error

Based on the above content, we can also understand when to use in and out:

  • A parent class generic object can be assigned to a subclass generic object. in is used;

  • A subclass generic object can be assigned a value to the parent class generic object, with an out value.

Original ENGLISH: In and out type variant of Kotlin

Recommended for the old article: Use Kotlin to develop Part 1 of the modern Android Project

▼ Click to read the original text to get the link

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger. Http://blog.csdn.net/IaC743nj0b/article/details/79017052

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.