The use of the combined syntax of Java reusable classes (with source code)

Source: Internet
Author: User

Objective

Reusing code is one of the many compelling features of Java . But to be a very revolutionary language, it's not enough to just copy and change the code, and it has to be able to do more.

The above methods are often used in the process-type language of C , but the results are not very good. As with everything in Java , problem solving revolves around classes. You can reuse code by creating a new class without having to write from scratch. You can use classes that others have already developed and debugged.

The trick to this approach is to use classes without breaking existing program code. Readers will see two ways to achieve this in the next article. The first method is very intuitive and simply produces an object of an existing class in the new class. Because the new class is made up of objects of existing classes, this method is called a combination. This method simply re-uses the functionality of the existing program code, not its form. (This is the analysis of this combination method)

The second method is more nuanced, and it creates a new class according to the type of the existing class. Instead of changing the form of an existing class, take the form of an existing class and add new code to it. This magical approach is called inheritance, and the compiler can do most of the work. Inheritance is one of the cornerstones of object-oriented programming, and the next section will do the necessary parsing.

In terms of composition and inheritance, the syntax and behavior are mostly similar. Because they are generating new types from existing types, this is very meaningful. In the next two sections, the reader will learn about these two code reuse mechanisms.


Sample source Code
In fact, in our routine programming we will often use the combination of technology, but we do not pay attention to it. Simply place the object reference in a new class. For example, suppose you need an object that has multiple Stringobjects, several basic types of data, and objects of another class. For non-primitive types of objects, you must place their references in a new class, but you can define the basic type data directly:
Package Com.mufeng.theseventhchapter;public class Watersource {private String s;public Watersource () { System.out.println ("Watersource ()"); s = "Constructed";} @Overridepublic String toString () {return s;}}

Package Com.mufeng.theseventhchapter;public class Sprinklersystem {private String valve1, Valve2, Valve3, Valve4; Private Watersource Source = new Watersource ();p rivate int i;private float f; @Overridepublic String toString () {return ' VA Lve1= "+ valve1 +" valve2= "+ valve2 +" valve3= "+ valve3+" valve4= "+ valve4 +" \ n "+" i= "+ i +" f= "+ f +" \ n "+" so Urce= "+ source;} public static void Main (string[] args) {Sprinklersystem Sprinklersystem = new Sprinklersystem (); System.out.println (Sprinklersystem);}}

Output results
Watersource () valve1=null valve2=null valve3=null valve4=nulli=0 f=0.0source=constructed

SOURCE parsing
Of the methods defined in the above two classes, one is special: toString ()。 Each non-basic type of object has a toString ()method, and when the compiler needs a StringWhen you have only one object, the method is called. So in sprinklersystem.tostring ()In an expression:
"Source=" + source
The editor will know that you want to have a StringObject ("source=")With WatersourceAdd. Since only one StringObject and another Stringobject, so the compiler tells you: "I'm going to call toString (), put SourceConverted into a string! "After doing this, it will be able to put two of StringConnect to and pass the results to System.out.println ()。 Whenever you want to make the class you create behave like this, you only need to write a toString ()Method can be used.






The use of the combined syntax of Java reusable classes (with source code)

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.