Thoughts on Java Overloading

Source: Internet
Author: User

This article is a personal understanding of how to reload methods in Java.

The emergence of overload has its practical significance: in many cases, different operations are essentially the same in performance. It is precisely because the actual operations are different that determine the specific implementation of the method, which requires writing differentCodeBut the performance is the same, which leads to the fact that these methods have the same meaning and can be expressed by a name. For example, for method implementation, the addition of two integer numbers is different from the addition of two real numbers. Different types of variables need to be defined, but they are all addition functions, what we achieve is the sum of two numbers. We are used to treating them as no difference in our lives. For the Java-like facial object language, he hopesProgramPersonnel can program in the way they think, rather than the way they think in the computer. This laid the foundation for the emergence of heavy load. We can think of the same function in our life as the same name in the method implementation, only in the parameter list to distinguish. It is natural that there are differences in the parameter list. Since this requires specific implementation of different functions, the type or number of parameters used for operation will be different, even if the parameter type and quantity are the same, you can adjust the parameter order to differentiate the parameter list.

For constructor methods, overloading is more in line with human thinking. Constructor is used to create an object. An object has two members: attribute and method. Attributes are different for different objects. Several objects correspond to several groups of attributes belonging to different objects, and several groups of different memory units are allocated in the memory for storage, these attributes do not affect each other. With this understanding, let's start by thinking that we define different objects of the same class because they are actually different. These objects have the same abstract attributes and functions, these objects belong to the same class, but these objects have different static descriptions and dynamic behaviors. For example, all books have attributes such as author, ISBN, price, publication date, version, publisher, purchase date, owner, and usage, each book has its own static description. For example, if the title is different, or the title is the same but the author is different, the title and the author are the same, but the publishing house are different. At the very least, the same person may have several identical books, however, after all, these specific objects have different descriptions, at least in different geographic locations. These specific objects are manufactured by us in real life. in Java, all kinds of objects are created by adding the new keyword to the constructor, when we are new to different objects, these objects should be able (CAN) to be "manufactured" using different constructor methods ". We can assign different initial attributes to different objects through constructors. To achieve this, we can use the same constructor to pass different parameter values, you can also use different constructors. From this perspective, the overload of constructor is essential for Object-Oriented.

From this perspective, heavy load is very necessary for Object-Oriented applications. In this way, we can avoid rote memorization.

The breakthrough of object-oriented programming is that we can write programs in our own way of thinking, and consider the syntax rules and features in Java from the ideological transformation, which can help us better learn Java.

Copy search

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.