Java Reusable Classes

Source: Internet
Author: User

Java reusable class called reusing classes , re-use of the class, reuse means the reuse of the class, its implementation is our usual use of the combination and inheritance;

  1, Combination : has-a relationship (self-understanding: The combination is we usually create a new class of objects, and then use the various properties of this class, which is the combination, the combination can only use its methods, can not change the method)

  2, inheritance : is-a relationship (self-understanding: Inheritance is a class to inherit another class, and then be able to use the methods and properties in this class, and be able to override the method in the parent class, is the interface)

  
  rewrite:(@Override) overrides are methods that subclass methods re-overwrite the parent class, and if there is no override or overload, then the subclass calls a method that does not have a subclass, but actually calls the parent class.

One of the @override annotations prevents accidental overloading when you do not want to reload.

Overriding note differs from overloading, where overloading is the same class, the method name is the same, and the method parameter differs to differentiate the method to invoke.

  ToString () method description : Calling an object can return a string to you, such as System.out.println (object);(object is a specific objects) this sentence will call the ToString () method, and then return

A string.

  final keyword : With the basic type of the final keyword, it means that the variable will not change after it is initialized. Like the C-Define, you want a variable in this program where this value does not need to be changed. You can use final.

       Static initialization, which is used in conjunction with final, occupies a storage space that cannot be changed.

When the final method is modified, it can be inherited, but cannot be modified, but cannot be rewritten;

The final modifier class is that this class cannot be inherited;

  Main () method description : We can set a main () method for each class, even if one class nested within another class is also possible, which makes unit testing for each class easier.

  

1 /**2 * Main () method description3 * 2016/5/44  **/ 5  PackageCN. java_7;6 7  Public classTextextendstext_1{8 9      Public Static voidMain (string[] args) {TenSystem.out.println ("Text"); OneText_1 text_1 =Newtext_1 (); A Text_1.main (args); -     } - } the  - classtext_1{ - text_1 () {} -      Public Static voidMain (string[] args) { +System.out.println ("Text_1"); -     } +}
View Code

Combination Inheritance Rewrite instance:

    

1 /**2 * Reusable class combinations and inheritance3 * 2016/5/44  */5  PackageCN. java_7;6 7 classfather{8     intAge ;9 Father () {Ten          This. Age = 50; OneSystem.out.println ("Father class:Father.age" + This. age); A     } -  -      Public voidget () { theSystem.out.println ("Father.get"); -     } -  -      PublicString toString () { +         return"Father.tostring"; -     } + } A  at //Inheritance - classSonextendsfather{ -     intAge ; - Son () { -          This. Age = 20; -System.out.println ("Son class:Son.age" + This. age); in     } -  to @Override +      Public voidget () { -System.out.println ("Son.get"); the     } *  $      PublicString toString () {Panax Notoginseng         return"Son.tostring"; -     } the } +  A //Inheritance the classGrandsonextendsson{ +     intAge ; -Grandson (intAge ) { $          This. Age =Age ; $System.out.println ("Grandson Class:grandson ' is" + This. age); -     } - } the  Public classFather_son { -      Public Static voidMain (string[] args) {Wuyi     //combination Syntax theFather Father =NewFather (); - System.out.println (father); Wu father.tostring (); -  AboutSon son =NewSon (); $ System.out.println (son); - son.tostring (); -  -Grandson grandson =NewGrandson (4); A System.out.println (grandson); +     } the}
View Code

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.