Java Package and inner class

Source: Internet
Author: User
Tags modifiers

1. Overview of Packages

Package, is actually a folder.

The purpose of the package is to classify the classes.

Package Division: By function and according to the module division.


2. Package Definition and Precautions

Package Definition: Package name;

Multi-level package names are separated by a. Number.

Precautions:

The package statement must be the first executable code of the program.

The package statement can only have one in a Java file.

If there is no package, the default is to have no packages name.

Package Cn;public class HelloWorld {public static void main (string[] args) {System.out.println ("Hello World");}}

Compile and run with package:

Manual type:

A: Write a Java file with a package

B: Compile the Java source file with the Javac command

C: Create the package name manually

D: Put the B-step class file to the bottom of the C-step package

E: Go back to the package root directory in the same directory where and then run

F: Run Java CN with package. HelloWorld

Auto-type:

A:javac when compiling with-D

Javac-d. Helloworld.java

B: Execute with java command, same as manual, with package name

Java CN. HelloWorld


3. Access between classes under different packages

Package cn;/** * Summation */public class Demo {public int sum (int a,int b) {return a + B;}}
Package com;/** * Tests */public class Test {public static void main (string[] args) {CN. Demo d = new CN. Demo ();//It's too much trouble. System.out.println (D.sum (1, 2));}}


4. Guide Package

Guide Package Overview: Access between classes under different packages, we find that each time you use a class under a different package, you need to add a full path to the package. Good trouble, this time, Java provides the function of the guide package.

Guide Package Format:

Import package name;

Note: The name of the Guide class is imported this way.

Package cn;/** * Summation */public class Demo {public int sum (int a,int b) {return a + B;}}
Package Com;import CN. demo;/** * Test */public class Test {public static void main (string[] args) {Demo d = new demo (); System.out.println (D.sum (1, 2));}}


5.package, import, class there is no order

Pcakge>import>class

Package: There can only be one

Import: You can have more than one

Class: There can be multiple, but if class is modified with public, then there is only one


7. Permission modifiers


Public Protected Default Private
In the same class
Same bun class, other class
different bun types

Different packages other classes



8. Modifiers that can be used for classes and their composition

Modifier:

Permission modifiers: private, default, protected, public

Status modifiers: Static, Final

Abstraction modifier: Abstract

Class:

Default modifier public

Final

Abstract

Member variables:

Private default protected public

Static final

Construction method

Private default protected public

Member Methods

Private default protected public

Static final

Abstract


9. Internal class overview and accessibility features

Inner class overview: Defining a class within other classes is called an inner class.

Access characteristics of internal classes:

Inner classes can directly access members of external classes, including private.

An external class must create an object to access the members of the inner class.

Package cn;/** * Internal class overview: * Class is defined inside other classes, and this class is called an inner class * For example: A class B is defined in Class A, and Class B is an inner class. * Internal class Access features: * 1. Inner classes can access members of external classes directly, including private * 2. External classes to access members of an inner class, you must create an object *///outer class class outer{private int num = 10;//inner class inner{p ublic void Show () {System.out.println (num);}} public void Method () {new Inner (). Show ();}} public class Innerclassdemo {public static void main (string[] args) {}}


10. Location of the inner class

Depending on where the inner class is defined in the class, it can be divided into two formats: 1) member position (member inner Class) 2) local location (local inner Class)

member Inner class:

How the outside world creates objects: external class names. Internal class Name Object name = External Class object. Inner class object;

Package cn;/** * The location of the inner class * member Location: The internal class defined in the member location, called the member inner class * Local location: The inner class defined in the local location, is called the inner class of the local * member internal class: * How to directly access members of the inner class? * External class name. Internal class Name Object name = External Class object. Inner class object; */class outer{private int num = 10;//member variable//member position defined in the member position of the inner class, called the member inner Class Inner{public void Show () {System.out.println ( num);}} public void Method () {//local position defines the inner class in the local location, called the local inner class Inner{}system.out.println ("member method");}} public class InnerClassDemo2 {public static void main (string[] args) {//I want to access the show () method of the Inner class//external class name. Internal class Name Object name = External Class object. Inner class object; Outer.Inner oi = new Outer (). New Inner (); Oi.show ();}}



11. member Inner Class

In general, in real-world development, members ' inner classes are not accessed directly outside.

Package com;//Body class body{//Heart class Heart{public void operator () {System.out.println ("Do heart bypass Surgery");}} public class Innerclassdemo {public static void main (string[] args) {/** * This is accessible to the operator method, but, ask, can anyone perform a heart bypass operation on you? In this case, either the heart bypass surgery is as simple as the */body.heart, or we are crazy. BH = new Body (). new Heart (); Bh.operator ();}
Package com;//Body class body{//heart private class Heart{public void operator () {System.out.println ("Do heart bypass Surgery");} Provides a way to verify the public void Test (String job) {if (Job.equals ("heart Surgeon")) {new Heart (). operator ();}}} public class Innerclassdemo {public static void main (string[] args) {Body BODY = new Body (); Body.test ("Heart Surgeon");}

Modifiers for members inner classes:

Private: is to ensure the security of the data.

Static: To make data access more convenient

A member inner class that is statically decorated can only access static members of an external class.

Internal classes are statically modified methods that can have static and non-static methods.

Package com;class outer{private int num = 10;private static int num2 = 100;//Inner class is decorated with a shaking because an inner class can be considered a member of an external class public static class Inner{public void Show () {System.out.println (num2);} public static void Show2 () {System.out.println (num2);}}} public class Innerclassdemo {public static void main (string[] args) {////member inner class is statically decorated after access mode//format: external class name. Internal class Name Object name = new External class name. Internal class name ( ); Outer. Inner oi = new Outer.Inner (); Oi.show (); Oi.show2 ();//show2 () Another way to call Outer.Inner.show2 ();}}
Package cn;/** * Outputs 30 20 10 respectively * NOTE: * 1. Internal classes and external classes have no inheritance relationship * 2. Qualifying this object with an external class name */class outer{public int num = 10;class Inner{pu blic int num = 20;public void show () {int num = 30; SYSTEM.OUT.PRINTLN (num); System.out.println (This.num); System.out.println (Outer.this.num);}}} public class Innerclasstest {public static void main (string[] args) {Outer.Inner Oi = new Outer (). New Inner (); Oi.show ();}}


12. Local Inner class

A local inner class that can directly access members of an external class.

A local inner class that can create an inner class object that invokes internal methods through an object to use the local inner class functionality.

Considerations for local internal classes accessing local variables:

Final keyword must be added

    Why? .

package com;/** *  local inner class  *  can directly access members of an external class  *  in a local location, you can create an inner class object, invoke an inner class method through an object, To use local inner class features  *  *   local internal classes to access local variables considerations?  *    local internal class access local variables must be decorated with the final keyword  *    because the local variable exists as a method call and disappears as the method call finishes. The objects in the heap memory do not disappear immediately, *    so we use the final keyword to modify the local variables. If the final modification, the variable becomes a constant.  *    even though the method call is complete, data 20 is stored in memory.  *    so even if the method call is complete, the objects in the heap memory are not immediately reclaimed by the garbage collector.  *    */class outer{private int num = 10;public void  method () {//local internal class access local variables must be decorated with the final keyword Final int num2 = 20;class inner{public  void show () {///can directly access members of the external class System.out.println (Outer.this.num);// Local internal class access local variables must be decorated with the final keyword System.out.println (num2);}} In a local location, you can create an inner class object that invokes an inner class method through an object to use the local inner class function New inner (). Show ();}} Public class innerclassdemo {public static void main (String[] args)  { Outer o = new&nBsp;outer (); O.method ();}} 


13. Anonymous Inner class

Anonymous inner class: In fact, it is the simplification of the inner class.

Premise: There is a class or interface, where the class can be either a concrete class or an abstract class.

package cn;/** *  anonymous inner class: *  is a simplified notation for inner classes.  *  *  premise: There is a class or interface  *   the class here can be either a concrete class or an abstract class   * *  format:  *  new  class name or interface name () { *  overriding method (); * } *  essence is an anonymous object that inherits the class or implements the interface subclass.  */interface inter{public void show ();p ublic void show2 ();} Class outer{public void method () { new inter () {@Overridepublic  void show ()  {system.out.println ("Show");} @Overridepublic  void show2 ()  {system.out.println ("Show2");}  }.show ();  new inter () {@Overridepublic  void show ()  {system.out.println ("Show1");} @Overridepublic  void show2 ()  {system.out.println ("Show2");}   }.show2 ();//Polymorphic  inter i = new inter ()  {@Overridepublic  void  show2 ()  {system.out.println ("Show2");} @Overridepublic  void show ()  {system.out.println ("Show");}; I.show (); &nBSP;}} Public class innerclassdemo {public static void main (String[] args)  { Outer o = new outer (); O.method ();}}


14. Use of anonymous internal classes in real-world development

Package com;/** * Anonymous inner class used in actual development * */interface person{public Void Study ();} Class Student implements person{@Overridepublic Void study () {System.out.println ("student Learning");}} Class persondemo{/** * Interface name as formal parameter * In fact, this is not the interface, but the implementation class of the interface * @param p */public void method (person p) {P.study ()}} public class Innerclasstest {public static void main (string[] args) {//test Persondemo PD = new Persondemo ();pd. Metho D (New Student ());}}
Package com;/** * Anonymous inner class used in actual development * */interface person{public Void Study ();} Class Student implements person{@Overridepublic Void study () {System.out.println ("student Learning");}} Class persondemo{/** * Interface name as formal parameter * In fact, this is not the interface, but the implementation class of the interface * @param p */public void method (person p) {P.study ()}} public class Innerclasstest {public static void main (string[] args) {persondemo PD = new Persondemo ();pd. Method (New Studen T ()); System.out.println ("-------------");//The use of anonymous internal classes in development//anonymous inner classes are inherited classes or subclass anonymous objects that implement an interface Pd.method (new person () {@ overridepublic Void Study () {System.out.println ("Teacher Learning"),}});}}

This article is from the "11831428" blog, please be sure to keep this source http://11841428.blog.51cto.com/11831428/1859057

Java Package and inner class

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.