Skew and resistance of generic Interfaces

Source: Internet
Author: User

Skew and resistance of generic Interfaces

1. Glossary

(1) covariant: the object of the parent class is replaced by a subclass.

(2) Resistance to changes: the subclass object is replaced by the parent class.

For example, if the parameters of the method are covariant, the return value is variable-resistant.

2. Coordination and resistance of generic Interfaces

(1) covariant: IDemo <out T >== "IDemo <out ParentT>

Generic Type T can only be used as the return value of methods or attributes in IDemo

 

(2) Resistance to changes: IDemo <in ParentT >== "IDemo <in T>

Generic Type T can only be used as a parameter of the method in IDemo

Simply put, we can understand from the word out and in: out indicates that the generic type can only be returned, and in indicates that the generic type can only be used as input.

3. Why?

Changing from T to ParentT is type-safe, and vice versa.

 

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.