C # confusing 46: confusing constructor case

Source: Internet
Author: User

Puzzle 46: confusing constructor Cases
This puzzle presents two easily confusing constructors. The main method calls a constructor, but which one does it call? TheProgramThe output depends on the answer to this question. So what does it print? Is it even legal?
Class confusing
{
Confusing (Object O)
{
System. Console. writeline ("object ");
}

Confusing (double [] darray)
{
System. Console. writeline ("Double array ");
}

Static void main ()
{
New confusing (null );
}
}

Confusing 46: confusing constructor case
The parameter passed to the constructor is an empty object reference. Therefore, it seems that the program should call the overloaded version of the parameter type object and print the object. Array is also a reference type, so null can also be applied to the overloaded version of double. You may come to the conclusion that this call is ambiguous and the program should not be compiled. If you try to run the program, you will find that these intuitive feelings are not correct: the program prints a double array. This behavior may seem unreasonable, but there is a good reason to explain it.
Java's heavy-load parsing process runs in two phases. In the first stage, select all available methods or constructors that can be applied. The second stage selects the most accurate method or constructor in the first stage. If a method or constructor can accept any parameter passed to another method or constructor, so we can say that the first method is less accurate than the second method (for the C # Heavy Load decision, see [C # Language Specification 7.4.2]. The main meaning is similar ).
In our program, both constructors are available and applicable. Confusing (object) can accept any parameter passed to confusing (double []), so confusing (object) is relatively inaccurate. (Every double array is an object, but every object is not necessarily a double array .) Therefore, the most accurate confusing is confusing (double []), which explains why the program produces such output.
If a value of the double [] type is passed, this behavior makes sense. But if you pass null, this behavior is contrary to intuition. The key to understanding this puzzle is When testing which method or constructor is the most accurate, these tests do not use real parameters: That is, the parameter that appears in the call. These parameters are only used to determine which overloaded version can be applied. Once the compiler determines which overloaded versions are available and applicable, it selects the most accurate version, and only uses the form parameter: The parameter that appears in the Declaration.
To use a null parameter to call the confusing (object) constructor, you need to write Code : New confusing (object) null ). This ensures that only the confusing (object) can be applied. More generally,To force the compiler to select an exact overloaded version, you need to convert the real parameter to the type declared by the form parameter.
It is rather unpleasant to choose among multiple overloaded versions in this way. In your API, make sure that the client is not allowed to take this extreme. Ideally, you should Avoid overloading: Use different names for different methods. Of course, sometimes this cannot be implemented. For example, the constructor does not have a name and thus cannot be assigned a different name. However, you can mitigate this problem by setting the constructor as private and providing public static factories. If the constructor has many parameters, you can use the builder mode to reduce the demand for overloaded versions.
If you have indeed carried out the overload, make sure that the parameter types accepted by all the overloaded versions are incompatible, so that no two overloaded versions can be applied at the same time. If this is not done, make sure that all the overloaded versions that can be applied share the same behavior.
In short, the parsing of overloaded versions may produce confusion. Avoid overloading as much as possible. If you must reload, you must follow the above guidelines to minimize such confusion. If an poorly designed API forces you to select between different overloaded versions, convert the real parameters into the type of the parameters of the overloaded version you want to call.

C # directory

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.