Older Dick Silk Self-study note--java 0 basics to rookie--020

Source: Internet
Author: User

packages, permission modifiers, inner classes, exercises

1. Package

Package: A folder that categorizes classes for management.

Package Format: Package name, multi-level pack "." Separated, such as: Packages Asia.persia.

Compile with package file: Create the appropriate folder manually, or compile the command with "-D", such as: javac-d Liverpool.java.

Access class files in different packages: class files are preceded by the full path of the package, such as: Asia.persia.Demo de=new Asia.persia.Demo ();.

Import the package using the "import" keyword.

Import: Imports the package, imported package name, multi-level pack "." Separated, such as: Import Asia.persia, then you can directly demo De=new demo ();

Note: The package must be the first execution statement of the program, and there are no packages to indicate that there is no packet name.

One file, the package can have only one, import can have more than one, class can have more than one (recommended).

2, permission modifier.

3. Inner class

The concept of an inner class: a class defined in a class, such as: Class C1{class c2{}}.

Intrinsic class features: Inner classes can access members of external classes directly, and external classes need to create objects to access members of the inner class.

Classification of inner classes: member inner class, local inner class.

1 classa{2     classb{3         //member Inner class4     }5 6      Public voidcod5 () {7         classc{8             //local inner class9         }Ten  OneC c=NewC ();//accessing local inner classes A         //c. Functions of local inner classes -     } -  the      Public voidcod2 () { -B b=NewB ();//accessing member internal classes -     } - } +  - classabc{ +      Public voidasdf () { AA.B ab=NewA ().NewB ();//accessing member internal classes atAb.cod5 ();//accessing local inner classes -     } -}

member inner class with this.

1 classfin{2     intNum=1;3 4     classen{5         intnum=2;6 7          Public voidShow () {8             intNum=3;9 Ten System.out.println (num); OneSystem.out.println ( This. Num); ASystem.out.println (Fin. This. Num); -         } -     } the } -  - //Output: 3,2,1

Local internal classes Access local variables, local variables must be final decorated, or life cycle error.

1 classfin{2      Public voidmethod{3         Final intNum=1;4 5         classen{6              Public voidShow () {7 System.out.println (num);8             }9         }Ten     } One}

Note: Static internal classes can only access static members of external classes; Static internal classes define static, non-static methods.

4. Exercises

Answer

1 Interfaceinter{2     voidShow ();3 }4 5 classouter{6     //The code is padded7      Public StaticInter Method () {8         return NewInter () {9              Public voidShow () {TenSystem.out.println ("Helloword"); One             } A         } -     } - } the  - classouterdemo{ -      Public Static voidMain (string[] args) { - Outer.method (). Show (); +     } - } +  A //Output: Helloword

Older Dick Silk Self-study note--java 0 basics to rookie--020

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.