Practical Significance of delegated type covariant and Inverter

Source: Internet
Author: User

Set a delegate typea1 delegatedefine (typeb1 B) and the actual call delegate Method

Typea2 delegateinstance (typeb2 B)
{
}
According to the principle of Object-oriented Inheritance and use, delegateinstance should be assigned to the delegatedefine delegate variable for the caller to call. For the delegate, it is equivalent to a public standard and the return type is typea, the actual return type must be a subclass of typea or typea. That is to say, for the delegate return type, the actual return type of the delegate method must be its subclass, that is to say, typea2 must be a subclass of typea1, which is the so-called covariant of the return type. for the typeb1 type of delegate parameter B, this actually tells the actual delegate method. The type I gave you is typeb1, because it is received by you, therefore, the actual method type must be typeb1 itself or the parent class of typeb1, that is, typeb2 must be the parent class of typeb1, this is the so-called commissioned parameter type inverter (some are called variable resistance ). as you know, delegation is actually equivalent to an interface or a standard. As a standard, the larger the scope, the better. from the above analysis, we can know that the larger the scope, the better, the higher the return type in the class hierarchy when defining the delegate. The better, the lower the parameter type, the better. This allows more methods to be provided to delegate calls.

From the perspective of the entrusting party, it is actually to define the standard, and the content type provided externally should be as specific as possible (the in parameter in the delegate definition is actually the content type provided by the entrusting party to the trustee ), in this way, more delegate methods can be involved, and the content types (return type and out parameter) accepted by the receiver are more abstract and better, because the content types accepted by the trustee are sent to the delegate, the more abstract the type is, the more room for participants.

 

PS: record is also a learning method.

 

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.