Overloading--object-oriented chicken ribs, strong-type language's weakness

Source: Internet
Author: User

The following remarks for the novice opinion, if there is inappropriate, please advise.

Overloading is a function/method within a class that defines several identical names but different parameters. As follows:

1 classbasketballplayer{2     PrivateString sex;3     PrivateString name;4      Public voidTest (String name) {5     ......}6      Public voidTest (String name,string sex) {7     ......}8}

But I personally feel that sometimes this function is more chicken, I might as well as the following to write a simple and convenient, unfortunately, the following is not used in Java, but also in my regular use of C + + and PHP support:

1 class basketballplayer{2     Private String sex; 3     Private String name; 4      Public void Test (String name,string sex="man") {5     ......} 6 }

Of course overloaded it also has practical time, such as:

int themax (int A,int  b) {    return a>b? a:b;} float themax (floatfloat  b) {    return a>b a:b;}

But I still think this is not a good way, the same function is only different data types will be written so many times the logic is exactly the same thing, is a waste of physical strength Ah, to modify also have to change one by one, perhaps to change the leak. I think a weak data type (such as PHP) is a better solution, and it automatically helps you handle different data types. As follows:

1 <? PHP 2 function Themax ($a,$b) {    //max is a system function is not available, so the name Themax3     return$a>$b$a$b; 4 }5 ?>

The current popular several strong-type language compiler will not, dare, would like to put "weak type" in it. It's hard to say, probably not for a long time. In particular, Java is more unlikely, some of the features are already there, it will be over n long, such as switch inside to put a string to the 1.7 version only. For now, the ultimate solution can only be used in weakly typed languages such as PHP,JS, and the strong type will have to suffer for a while in the reincarnation of copy-and-paste modification.

Overloading-object-oriented chicken ribs, strong-type language weakness

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.