Basic use of inner classes

Inner class concept

Define a class within a class. Example: If you define A class B inside A class A, class B is called an inner class

The inner class defines the format

Format & Examples:

Outer {public class Inner {}} */ class Outer {public class Inner {}}Copy the code

Access characteristics of inner classes

Inner classes can directly access members of outer classes, including private outer classes

To access members of an inner class, you must create objects

Sample code:

Public class Outer {private int num = 10; public class Outer {private int num = 10; public class Inner { public void show() { System.out.println(num); } } public void method() { Inner i = new Inner(); i.show(); }}Copy the code

Member inner class

The definition location of a class within a member

Methods in a class are created in the same place as member variables in the inner class format

 

Format: external class name. Inner class name Object name = outer class object. Inner class object; Outer.Inner oi = new Outer().new Inner();

 

Private member inner class

When a class is designed to be an inner class, most of them are designed to be inaccessible to the outside world, so the definition of the inner class should be privatized, and then a method that can be called by the outside world should be provided, and the inner class object will be created inside the method and called.

 

Sample code:

class Outer { private int num = 10; private class Inner { public void show() { System.out.println(num); } } public void method() { Inner i = new Inner(); i.show(); } } public class InnerDemo { public static void main(String[] args) { //Outer.Inner oi = new Outer().new Inner(); //oi.show(); Outer o = new Outer(); o.method(); }}Copy the code

Static member inner class

Static member internal class access format: external class name. Internal class Name Object name = new External class name. Inner class name ();

Static method in a static member inner class: the name of the outer class. Inner class name. Method name ();

The sample code

class Outer { static class Inner { public void show() { System.out.println("inner.. show"); } public static void method() { System.out.println("inner.. method"); Public static void main(String[] args) {// Public static void main(String[] args) {// Public static void main(String[] args) { Internal class Name Object name = new External class name. Inner class name (); Outer.Inner oi = new Outer.Inner(); oi.show(); Outer.Inner.method(); }}Copy the code

Local inner class

The local inner class defines the location

A local inner class is a class defined in a method

 

Local inner class mode

Local inner class, which cannot be used directly by the outside world, needs to create objects inside the method and use them

The class can access members of an external class directly, or local variables within a method

 

The sample code

class Outer { private int num = 10; public void method() { int num2 = 20; class Inner { public void show() { System.out.println(num); System.out.println(num2); } } Inner i = new Inner(); i.show(); } } public class test { public static void main(String[] args) { Outer o = new Outer(); o.method(); }}Copy the code

Anonymous inner class

The premise of anonymous inner classes

There is a class or interface where the class can be concrete or abstract

 

The format of anonymous inner classes

Format: new class name () {override method};

New interface name () {override method};

Notice the semicolon after the braces at the end!!

 

For example:

New Override public void method(){}}; // Notice the semicolonCopy the code

The nature of anonymous inner classes

Essence: Details of an anonymous inner class of an anonymous object that inherits the class or subclasses that implement the interface

Anonymous inner classes can be accepted in polymorphic form

Inter i=new Inter(){ @Override public void method(){ }; // Notice the semicolon}Copy the code

Anonymous inner classes call methods directly

 

interface Inter { void method(); } class Test { public static void main(String[] args) { new Inter() { @Override public void method() { System.out.println(" I am an anonymous inner class "); } }.method(); // Call the method}}Copy the code

Use of anonymous inner classes in development

Use of anonymous inner classes in development

When we find that a method needs an interface or a subclass object of an abstract class, we can pass in an anonymous inner class to simplify the traditional code

 

The sample code

/* interface Swimming {void swim(); } public class TestSwimming { public static void main(String[] args) { goSwimming(new Swimming() { @Override public void Swim () {system.out.println (" swim, let's go "); }}); } public static void goSwimming(Swimming Swimming) {/* Swimming swim = new Swimming() {@override Public void swim() {system.out.println (); } } */ swimming.swim(); }}Copy the code