Java Reusable Classes

Source: Internet
Author: User

Java Reusable Classes

There are two common methods of Java reuse classes.

One, combination: An object that produces an existing class in a new class. Because the new class is made up of objects of existing classes, this method becomes a combination.

Import Java.util.*;class watersource{private String s; Watersource () {System.out.println ("Watersource ()"); s= "constructed";} Public String toString () {return s;}} public class Sprinkersystem{private String value1,value2,value3,value4;private watersource source=new WaterSource (); private int i;private float f;public String toString () {return "value1=" +value1+ "" +   "value2=" +value2+ "" + "Valu   e3= "+value3+" "+   " value4= "+value4+" \ n "+   " i= "+i+" "+" f= "+f+" "+" source= "+source   ;} public static void Main (string[] args) {Sprinkersystem sprinker=new sprinkersystem (); System.out.println (Sprinker);}}

Initialization

1, where the object is defined.

2, in the constructor of the class.

3, before you are ready to use these objects.

4, use instance initialization.

Second, inheritance: 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.

When a class is created, it is always inherited, so unless explicitly stated to inherit from another class, it is implicitly inherited from the Java standard root class object.

Import Java.util.*;class cleanser{private string s= "Cleaner";p ublic void Append (String a) {s+=a;} public void Dilute () {Append ("dilute ()");} public void Apply () {Append ("Apply ()");} public void Scrub () {Append ("scrub ()");} Public String toString () {return s;} public static void Main (string[] args) {Cleanser X=new Cleanser (); X.dilute (); x.apply (); X.scrub (); SYSTEM.OUT.PRINTLN (x);}} public class detergent extends cleanser{public void scrub () {Append ("Detergent.scrub ()"); Super.scrub ();} public void Foam () {Append ("foam ()");} public static void Main (string[] args) {detergent x=new detergent (); X.dilute (); x.apply (); X.scrub (); X.foam (); SYSTEM.OUT.PRINTLN (x); System.out.println ("Test base class:"); Cleanser.main (args);}}

Initialize the base class: The build process spreads outwards from the base class.

Destroy object: The Java garbage collector frees its memory if necessary.

Name masking: @Override annotations can prevent you from accidentally overloading when you don't want to reload.

Upward Transformation: The Export class is a superset of the base class, which may contain more methods than the base class, and the only thing that can happen to the class interface in the process of upward transformation is the loss of methods, not the acquisition of them.


The choice between composition and inheritance:

Combinatorial techniques are typically used in situations where you want to use the functionality of an existing class in a new class rather than its interface. That is, embedding an object in a new class allows it to implement the required functionality, but the user of the new class can only see the interface defined for the new class, not the interface of the embedded object. The relationship of "is-a" is expressed by inheritance, and the relationship of "has-a" is expressed by combination, whether it is a combination or a succession, a clear method is to determine whether it is necessary to change from the new type to the base class. Inheritance is necessary if it has to be upward, but if it is not necessary, consider whether it really needs to be inherited.

Java Reusable Classes

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.