Java Programming Thought Reading notes _ 7th Chapter

Source: Internet
Author: User

The final keyword is similar to const:

1 ImportJava.util.*;2 3  Public classFinalData {4     StaticRandom Rand =NewRandom (47);5     Final intValueone = 9;6     Final intI4 = Rand.nextint (20);7     Static Final intInt_5 = Rand.nextint (20);8      Public Static voidMain (string[] args) {9FinalData F =NewFinalData ();Ten         //f.valueone = 7; One         //f.i4 = 7; A System.out.println (f); -          -FinalData F1 =NewFinalData (); the System.out.println (F1); -     } -      PublicString toString () { -         return +"Valueone =" +Valueone -+ "+" + "I4 =" +I4 ++ "+" + "Int_5 =" +int_5; A     } at}

Note the difference between I4 and int_5: in different objects of the class Int_5 is initialized only once when the object is created, and I4 is initialized once each object is created.

The principles of name masking and C + + are different

The overloaded functions of the C + + neutron class mask other functions in the base class:

1#include <iostream>2 3 classHomer {4  Public:5     CharDohCharc) {6Std::cout <<"Doh (char)"<<Std::endl;7         return 'D';8     }9     Ten     floatDohfloatf) { OneStd::cout <<"doh (float)"<<Std::endl; A         return 1.0f; -     } - }; the  - classMilhouse {}; -  - classBart:homer { +  Public: -     voidDoh (milhouse&m) { +Std::cout <<"Doh (Milhouse)"<<Std::endl; A     } at }; -  - intMain () { - Bart B; -     //B.doh (1); -B.doh ('x'); in     //B.doh (1.0f); - Milhouse m; to B.doh (m); +     return 0; -}

and Java does not

1 classHomer {2     CharDohCharc) {3System.out.println ("Doh (char)");4         return' d ';5     }6     7     floatDohfloatf) {8System.out.println ("Doh (float)");9         return1.0f;Ten     } One } A  - classMilhouse {} -  the classBartextendsHomer { -     voidDoh (Milhouse m) { -System.out.println ("Doh (Milhouse)"); -     } +      -@OverrideCharDohCharc) { +System.out.println ("Bart:doh (char)"); A         returnC; at     } - } -  -  Public classHide { -      Public Static voidMain (string[] args) { -Bart B =NewBart (); inB.doh (1); -B.doh (' x '); toB.doh (1.0f); +B.doh (NewMilhouse ()); -     } the}

Also, be aware of the initialization sequence.

1 classInsect {2 insect () {3System.out.println ("Insect::insect ()");4     }5     6     Static inti = Printinit ("Static Insect::i");7     8     Static intPrintinit (String s) {9 System.out.println (s);Ten         return5; One     } A      -     intj = Printinit ("Insect::j"); - } the  -  Public classBeetleextendsInsect { - Beetle () { -System.out.println ("Beetle::beetle ()"); +     } -  +      Public Static voidMain (string[] args) { ASystem.out.println ("Beetle::main ()"); at         NewBeetle (); -     } -  -     intK = Printinit ("Beetle::k"); -     Static intL = Printinit ("Static Beetle::l")); -}

Java Programming Thought Reading notes _ 7th Chapter

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.