1, Internal class foundation
In Java, a class can be defined in another class or a method. Such a class is called an inner class. In a broad sense, internal classes generally include these four types: member internal classes, local internal classes, anonymous internal classes and static internal classes. Let's take a look at the usage of these four internal classes.
1. Member internal class
Member inner class is the most common inner class. Its definition is located inside another class, as shown in the following form:
class Circle { double radius = 0; public Circle(double radius) { this.radius = radius; } class Draw { //Inner class public void drawSahpe() { System.out.println("drawshape"); } } }
In this way, the Draw class looks like a member of the Circle class, which is called an external class. The inner class of a member unconditionally accesses all member properties and member methods (including private and static) of the outer class.
class Circle { private double radius = 0; public static int count =1; public Circle(double radius) { this.radius = radius; } class Draw { //Inner class public void drawSahpe() { System.out.println(radius); //private member of external class System.out.println(count); //Static members of external classes } } }
However, please note that when the member variable or method of the internal member class is the same as the name of the external class, hiding will occur, which means that the members of the internal class are accessed by default. If you want to access members of an external class with the same name, you need to access it in the following form:
External class.this.Member variable External class.this.Member method
Members of the inner class can access members of the outer class unconditionally, while the outer class cannot access members of the inner class so freely. To access members of an inner class in an outer class, you must first create an object of the inner class, and then access it through a reference to the object:
class Circle { private double radius = 0; public Circle(double radius) { this.radius = radius; getDrawInstance().drawSahpe(); //You must create an object of a member's inner class before accessing it } private Draw getDrawInstance() { return new Draw(); } class Draw { //Inner class public void drawSahpe() { System.out.println(radius); //private member of external class } } }
The inner member class depends on the outer class, that is, if you want to create an object of the inner member class, there must be an object of the outer class. The general method of creating class objects inside members is as follows:
public class Test { public static void main(String[] args) { //The first way: Outter outter = new Outter(); Outter.Inner inner = outter.new Inner(); //Must be created through an outer object //The second way: Outter.Inner inner1 = outter.getInnerInstance(); } } class Outter { private Inner inner = null; public Outter() { } public Inner getInnerInstance() { if(inner == null) inner = new Inner(); return inner; } class Inner { public Inner() { } } }
2. Local internal class
A local internal class is a class defined in a method or scope. The difference between it and a member internal class is that the access of a local internal class is limited to the method or scope.
class People{ public People() { } } class Man{ public Man(){ } public People getWoman(){ class Woman extends People{ //Local inner class int age =0; } return new Woman(); } }
Note that local inner classes, like local variables in methods, cannot have public, protected, private, or static modifiers.
Anonymous Inner Class
Anonymous inner classes are probably the most used classes when we write code. Using anonymous inner classes when writing code to listen to events is not only convenient, but also makes the code easier to maintain. The following code is an Android event listener:
scan_bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub } }); history_bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub } });
This code sets the listener for the two buttons, which uses the anonymous inner class. In this Code:
new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub } }
This is the use of anonymous inner classes. The code needs to set a listener object for the button. Using anonymous inner classes, you can generate corresponding objects when implementing methods in the parent class or interface, but only if the parent class or interface exists first. Of course, it is also possible to use anonymous inner classes to achieve the same effect as above.
private void setListener() { scan_bt.setOnClickListener(new Listener1()); history_bt.setOnClickListener(new Listener2()); } class Listener1 implements View.OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub } } class Listener2 implements View.OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub } }
4. Static internal class
Static internal classes are also classes defined in another class, but there is a keyword static in front of the class. Static internal classes do not need to depend on external classes. This is similar to the static member properties of classes, and it cannot use non static member variables or methods of external classes. This is well understood because objects of static internal classes can be created without objects of external classes. If non static members of external classes are allowed to access, there will be a contradiction, Because non static members of external classes must be attached to specific objects.
public class Test { public static void main(String[] args) { Outter.Inner inner = new Outter.Inner(); } } class Outter { public Outter() { } static class Inner { public Inner() { } } }