The famous Java inner classes and anonymous inner classes

Java inner classes

Class is defined in the outer class is an internal class; class is defined inside for greater clarity, described in detail things.

Famous inner classes and anonymous inner classes

Java language class name if there is an inner class is divided into well-known inner classes and anonymous inner classes (no class name) according to the internal classes.

Famous inner class and create objects

public class OutClass{

public void play(){

System.out.println ( " is playing ......");

}

public class Inner{

public void play(){

System.out.println ( " is happily playing ......");

}

}

 

/* public static void main(String[] args){

OutClass outer = new OutClass();

Inner inner = outer.new Inner (); // Create the name of the same class may be omitted in the outer

outer.play();

inner.play();

}

*/

}

public class Test{

public static void main(String[] args){

OutClass outer = new OutClass();

OutClass.Inner inner = outer.new Inner();

outer.play();

inner.play();

}

}

We are playing ......

We are happily playing ......

The example is a well-known internal class, the class name Inner format, the class object to create an internal: external name class . Inside class name object name = external object name .new internal class name;

There is also a static modification of static inner class, then create an object format: External class name . Internal name of the object class name = new outer class name . Internal class name;

public class OutClass{

public void play(){

System.out.println ( " is playing ......");

}

public static class Inner{

public void play(){

System.out.println ( " is happily playing ......");

}

}

/*public static void main(String[] args){

OutClass outer = new OutClass();

Inner inner = new OutClass.Inner (); // The same can be omitted, the external name class

outer.play();

inner.play();

}*/

}

public class Test{

public static void main(String[] args){

OutClass outer = new OutClass();

OutClass.Inner inner = new OutClass.Inner (); // Note: Creating a slightly different way

outer.play();

inner.play();

}

}

We are playing ......

We are happily playing ......

Anonymous inner class and create objects

Some classes we used only once is not required, if we deliberately create a class is more a waste of resources, while the anonymous inner class is used only once in order to achieve this class.

Because anonymous inner class is not a class name, so I can not create anonymous inner classes directly; define anonymous inner classes is also required to directly instantiate the class; its syntax is:

new parent class constructor ([ Parameter 1], [ parameter 2], ... [ parameter n]) {

            // anonymous inner class class body

}; // Note that there must be an end   ;   

// abstract class

public abstract class Father{

public abstract void work();

}

public class Son{

 

public static void main(String[] args){

Father father = new Father(){

public void work(){

System.out.println ( " attending classes ......");

}

};

father.work();

}

}

Attending classes ......

Interface configuration class defining anonymous also need to be instantiated; The syntax is:

new Interface () {

        // anonymous inner class class body

}; // Note that there must be an end   ;   

// Interface

public interface Computer {

void add(int a,int b);

}

public class Test{

public static void main(String[] args){

Computer software Computer = new () {

public void add(int a,int b){

System.out.println(a+b);

}

};

iccomputer.add(1,5);

}

}

note:

 · Anonymous inner class must implement the interface (which can only be achieved one interface) or subclass of class, in which new after the keyword class or interface name is the anonymous inner class inherits the parent class or implement an interface;

 · Methods anonymous inner class must not be abstract method;

 · Anonymous inner class new properties and methods can not be mobilized on the transformation of the object; anonymous inner class object can only be on a non-transformation of the way objects created by calling

public interface Computer {

void add(int a,int b);

}

public class Test{

public static void main(String[] args){

new Computer () {

public void add(int a,int b){

System.out.println(a+b);

}

public void computer(){

System.out.println ( " Counting ......");

}

}.computer();

 

}

}

Calculating ......

Static inner classes and non-static inner classes

According to class if there is static modifier modified, inner classes can be divided into static inner classes and non-static inner classes, because more than inner classes are non-static inner classes, so we just say the following static inner classes;

Static inner classes

public class Father{

static String name = "Tom";

String id = "888";

 

public static void work(){

System.out.println ( " working ......");

}

public void study(){

System.out.println ( " studying for ......");

}

static{

Son.work (); // call the static method static inner classes

System.out.println (Son.name); // call the static property static inner classes

Son son = new Son (); // create static inner class object

son.study (); // non-static inner class static method call by creating a static inner class object

System.out.println (son.id); // call non-static property static inner class by creating a static inner class object

}

public static class Son{

static String name = "Jim";

String id = "222";

public static void work(){

System.out.println ( " being in school ......");

}

public void study(){

System.out.println ( " are learning ......");

}

static{

Father.work (); // call the static method of the outer class

System.out.println (Father.name); // call the static properties of the outer class

Father father = new Father (); // create external object

father.study (); // non-static method of the outer class by creating an external call the class object

System.out.println (father.id); // call external class non-static properties by creating an external object

}

}

public static void main(String[] args){

new Father();

new Father.Son();

}

}  

Working ......

Tom

Further education ......

888

We are attending school ......

Jim

We are learning ......

222

Internal static type direct call external static properties and methods class, non-static properties and methods can be called by external classes to create external object;

Class external direct call static properties and methods within the class static, non-static properties and static methods can be called by an internal class to create an internal class object;

Note: static inner classes belong to the outer class, not part of the external object

Summary inner class feature

Inner class is a separate class, will generate a single compiled .class file; if it is well-known inner class, the class name is an external + $ + internal class name; if it is anonymous inner class, compared with an external class name + $ + digital;

Common classes, abstract classes and interfaces can have an internal class (including the famous inner and anonymous inner classes);

The outer class access specifier only MT4 Demo Account Tutorial public or default; but the inner class access control characters can be public , Protect , default , Private , you can also use static modification (Note: static can not be modified outside of class and interfaces);

Code blocks and methods defined in the internal class identifier not be modified by the access control, nor can the static modification;

Inner class can access the member variables outside of class for all access.

Internal class definition:

Class defined in a class called the outer inner classes.

Inner class classification:

Is there a name by: famous inner classes and anonymous inner classes.

According to whether there is static : static and non-static inner classes inside the class.

Famous inner class:

Create a form as follows:

public class Body {

public class Heart{

void work () {// This class is an internal class known, in general category Heart inside.

System.out.println ( " a beating ");

}

}

}

Anonymous inner classes:

1 , create

Because the need to create an object class names, not anonymous inner class class name, so when anonymous inner class definition object definitions and create together.

Create a form as follows:

new parent class constructor ([ parameter list ]) | Interface () {

Anonymous inner classes like body

};

Object object = new Object () { // object the object is anonymous inner classes.

};

2 , Features

anonymous inner class is a subclass of the general class:

Object object = new Object () { // this time anonymous inner class is Object subclass, regardless of which created in class.

};

anonymous inner class is a subclass of abstract class:

Test1.java

public abstract class Test1 {

public abstract void eat();

}

Test.java

public class Test {

public static void main(String[] args) {

Test1 test = new Test1()

{

public void eat() {

System.out.println("");

}

};

}

}

anonymous inner classes interface implementation class:

Test1.java

public interface Test1 {

void eat();

}

Test.java

public class Test {

public static void main(String[] args) {

Test1 test = new Test1()

{

public void eat() {

System.out.println("");

}

};

}

}

anonymous inner class can not be abstract:

If the anonymous inner class is an abstract class, it must have subclasses, but because of anonymous inner classes have no name, no sub-categories, so anonymous inner class can not be abstract.

Similarity:

1 , whether famous or anonymous inner classes inside class, javac will have a separate class files , after compiling inner classes are compiled into a separate .class file:

If the inside inner class class known, the known internal class bytecode file named :

If an anonymous inner class, the anonymous inner class bytecode file named ( last plus numbers ):

2 , if the direct internal class defined in the class, it uses similar manner as member variables:

 

        Body body = new Body();

System.out.println(body.age);

Heart heart = body.new Heart (); // increase the variable to call the parent class.

heart.work();

3 , the external interface class or external public default; but directly within the class defined in the class, there may be public Private ~ .

public class Test {

private class Test01

{

}

public static void main(String[] args) {

}

}

4 , if the values of local variables within the class, then: JDK8.0 and + may not apply final modified 8.0 before use must fianl .

public class Test {

public static void main(String[] args) {

int age = 12; // if 8.0 before must int plus front fianl .

Test1 test = new Test1()

{

public void eat() {

System.out.println(""+age);

}

};

}

}

————————————————

Original link: https://blog.csdn.net/qq_44971038/article/details/102798027

Guess you like

Origin www.cnblogs.com/benming/p/11770764.html