Summary of ②--internal classes in Java Learning Records

Source: Internet
Author: User

Speaking of internal class, just started a little dizzy, and then slowly read.

In fact, the internal class can also be compared to the phone, the class is a whole cell phone abstraction, class is a part of the abstract.

1. This is one of the simplest examples:

/** Below is an entire set of abstract mobile phones*/classphone{floatcpu= 1.2f; //internal class: The CPU class represents the abstract CPU inside the phone    classcpu{//what the CPU should be able to do        voidprint () {System.out.println ("Cpu works!"); }    }} Public classHelloWorld { Public Static voidMain (string[] agrs) {//turn the phone into a real iphonePhone IPhone =Newphone (); //The iphone's CPU is also manifestedPhone.cpu Gaotong = IPhone.NewCPU ();    Gaotong.print (); }}

2. What happens if the internal class and the external class variable have the same name?

classphone{//External CPU Variables    floatcpu= 1.2f; classcpu{//Internal CPU Variables        floatCPU = 1.5f; voidprint () {System.out.println ("Cpu works!"); //Nothing inside can be read directly without adding anything.System.out.println ( This. CPU); //external needs plus phone.thisSYSTEM.OUT.PRINTLN (phone. This. CPU); }    }} Public classHelloWorld { Public Static voidMain (string[] agrs) {phone IPhone=Newphone (); PHONE.CPU Gaotong= IPhone.NewCPU ();    Gaotong.print (); }}

The result is:

Cpu works!
1.5
1.2

Then found that access to internal variables do not need to operate (this does not add is also possible), but the external need to add phone.this

3. What if the inner class is static?

classphone{Static floatCPU = 1.5f; Static classcpu{floatCPU = 1.5f; voidprint () {System.out.println (CPU); //internal want to access external things, external must be staticSystem.out.println (PHONE.CPU); }    }} Public classHelloWorld { Public Static voidMain (string[] agrs) {/*is it different from what happened before? * Because the inner class is static, Phone.cpu is a whole * object can be new directly*/phone.cpu Gaotong=Newphone.cpu ();    Gaotong.print (); }}

The result is:

1.5
1.2

4. What about the inner class being private?

classPhone {Private floatCPU = 1.2f; Private classCPU {Private floatCPU = 1.5f; Private voidCpuprint () {System.out.println (CPU); }    }    //using a phone method, call the internal class CPU variable     Public voidprint () {NewCPU (). Cpuprint (); }} Public classHelloWorld { Public Static voidMain (string[] agrs) {//so there's no new inner class.Phone IPhone =Newphone ();    Iphone.print (); }}

The result is:

1.5

Summarize:

The benefit of the inner class is the ability to directly access the elements of the external class, even if it is private, as if the CPU can get the power of the phone directly.

Summary of ②--internal classes in Java Learning Records

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.