Java basic and important inner classes (important (anonymous inner classes))
catalogue 🐱 🚀
Overall concept
An inner class completely nested another class structure. The nested class is called inner class
Internal classes are very important and difficult. They are very important and helpful for us to view the underlying source code
Five members of class {attribute, method, constructor, code block, internal class}!!!! Need to know and remember
Basic grammar
class Outer{//External class class Inner{//Inner class } } class Other{//External other classes }
1. Local internal class (can be understood as local variable) 🐱 🐉
definition
1. The scope of the local internal class definition is in the method or in the code block!!!
2. The scope is in the method body or in the code block!!!!!!!! (if you go out of this range, you can't access it. Please note)
3. The local inner class is still a class!!!!!!!!!!
4. Local internal classes are properties that can directly access external classes, which can be private properties or other properties.
5. If the external class wants to access the members of the internal class, this is a little troublesome. The specific method is to create an internal class object in the method defining the external class, and then call the internal member through the instantiation object
attribute
package day05; public class LocalInnerClass { public static void main(String[] args) { Outer02 outer02 = new Outer02(); outer02.m1();//In this way, we call m1 directly to } } class Outer02{ private int n1=100; private void m2(){} public void m1(){ String name="Shandong"; //The scope of a local internal class is only in the method or code block that defines it (because its essence is that it has the same status in the method as the name we wrote, in fact). It cannot add an access modifier, but it can be modified with final //The local inner class is defined above the local position of the outer class, usually in the method area class Inner02{//A local inner class (essentially a class) can directly access all public void f1(){ //The local inner class can directly access the members of the external class, System.out.println("n1="+n1); m2(); } } Inner02 inner02 = new Inner02();//If we create an instance object here, we can implement our idea through operation processing of external classes inner02.f1(); //The external class can create the object of the local inner class in the method, and then call the method. } { //Local inner classes can also be written in code blocks class Inner03{ } } }
6. Other external classes are local internal classes that cannot be created
The reason is that the local internal class is equivalent to the nature of a local variable, and to be honest, this class can only be used in the method you create. You can't create it at all due to the simplification of that method. Therefore, other external classes cannot create local internal classes.
7. If the members of the external class and the local internal class have the same name, the proximity principle is followed by default. If we want to access the members of the external class, we need to take measures
External class name this. The following is a code demonstration
In depth analysis of this
In fact, it can be understood in this way, which is the in-depth understanding of this This actually represents the object pointing to this class,
So, in fact, outer This is an object. Of course, an object can directly call my internal properties
So how to verify? We know that the hashcode can reflect this address to a certain extent. If we print this address and create an object from the outside, the problem will be solved
We can understand it as class This is an object pointing to this class
2. Anonymous inner class 🐱 🏍
definition
Very important, very important when looking at the source code in the future
An anonymous inner class is essentially a class
2. It is also an internal class (defined in a method or in a code block)
3. The anonymous class has no name (in fact, it is assigned by the system when we can't see it)
**4. Anonymous inner class or an object** Because we use it to create objects
Writing format of anonymous inner class
Interface type variable name (){
//Method of rewriting interface
};
Type variable name of class (){
//Method of rewriting interface
};
1. Anonymous inner class interface based operation processing
package day06; public class AnoymousInnerClass { public static void main(String[] args) { Outer04 outer04 = new Outer04(); outer04.methond(); } } class Outer04{ private int age; public void methond(){//Use of methods //1. Requirements we want to use IA interface, implement the interface and create objects, //2. Our traditional method is to create a class and implement this method, but if we only use this class once and don't use this class after using it, then we //If we create a class, it will be effective. There is no need, so the anonymous inner class can come on stage at this time //3. Our requirement is to create a class. This class is only used once and will not be used later //4. tiger's compilation type is IA (see the left). What is the running type? You may say this. How do I know that it is not that there is no name, but that the name is allocated by the system, but not ourselves //In fact, the operation type is the anonymous internal class assigned to us by the system, which needs to be remembered!!!! //We do not assign values ourselves, so our anonymous inner class has a name //Let's go deep into the bottom layer of jdk to see what the specific implementation process looks like //The bottom layer is actually the name of the class that will allocate attributes, which is Outer04 . This operation is very magical, that is to say, our class name is this // /* class Outer04$1 implements IA{ @Override public void cry() { System.out.println("The tiger roared "); } */ IA tiger=new IA() {//Here, you may wonder how this interface can instantiate objects. In fact, that's not the case //We can see that the method is implemented directly //After this class is allocated, you can't find this class and create a new object, that is, you can't create another object @Override public void cry() { System.out.println("Tiger Growl ......"); } }; System.out.println(tiger.getClass()); } } interface IA{ void cry();//A method waits to be implemented }
To sum up, anonymous inner classes are actually operations to simplify the process. When we want to implement a class, if we just want to use it once and don't use it the second time, it will be wasteful if we create a new class, so anonymous inner classes are very important
And does the anonymous inner class really have no name???? In fact, it is not given to us by our bottom layer. Each anonymous inner class will be given a name, that is, it is not written by ourselves, but it does not exist. We will immediately return a type. In fact, it is
Anonymous inner class based on class
class Outer04{ //1. The compilation type of Father is Father //2. The operation type of father is Outer04 //3. The bottom layer will create anonymous inner classes /* class Outer04$2 extends Father{ @Override public void test() { System.out.println("Anonymous inner class ""); } } An object of the anonymous inner class is also returned */ private int age; public void methond(){//Use of methods Father father=new Father(){ @Override public void test() { System.out.println("Anonymous Inner Class "); } }; } } class Father{ public void test(){ } }
The syntax of anonymous inner class is quite strange. Please note that anonymous inner class is not only a class definition, but also an object. Therefore, from the perspective of syntax, it has the characteristics of both classes. There are also features for creating objects.
In fact, its precautions are almost the same as those of local internal classes, except for the writing format when the anonymous internal class is created
Usage scenarios of anonymous inner classes
This is mainly used when passing function parameters. For example, when we want to pass an object, we can pass it directly through our anonymous inner class,
Let's take a look at the code example
package day06; public class AnonymousInnerExise { public static void main(String[] args) { f1(new IL() {//It is easy to use when passing parameters to functions @Override public void show() { System.out.println("This is called the way of simplicity, which is very convenient");// } }); f1(new picture());//In this way, we can complete our operation, but it's a very blunt feeling } //The formal parameter is an interface type public static void f1(IL il){//In fact, when we use new, we will return the of an instance object as a parameter il.show(); } } //Interface interface IL{ void show(); } class picture implements IL{//Here is to create a class first. In this way, it seems very bloated and the code is very awkward! @Override public void show() { System.out.println("This is not the way of simplicity"); } }
A little exercise
describe
There is a ringtone interface. There is a mobile phone class with the function of alarm clock. The parameters are BELL type
//Task we want to test the alarm clock function of the mobile phone class, and print the lazy pig to get up through the anonymous internal class (object) as a parameter
//Pass in another anonymous inner class (object) and print the class
package day06; public class InnerClassExercise { public static void main(String[] args) { CellPhone cellPhone = new CellPhone(); cellPhone.alarmClock(new Bell() {//Here is the implementation of the anonymous inner class created directly @Override public void ring() { System.out.println("Lazy pig, get up"); } }); cellPhone.alarmClock(new Bell() {//Here is the incoming data that implements the Bell interface. Just put it in @Override public void ring() { System.out.println("The little friend has class"); } }); } } interface Bell{//There is a ringtone interface void ring(); } class CellPhone{// public void alarmClock(Bell bell){ bell.ring(); } }
The nature of anonymous inner classes
In fact, the anonymous internal class does not have a name. In fact, its name is assigned by the system. We can also view it. In fact, the operation type is the anonymous internal class assigned to us by the system. This needs to be remembered!!!!
//We do not assign values ourselves, so our anonymous inner class has a name
//Let's go deep into the bottom layer of jdk to see what the specific implementation process looks like
//In fact, the name of the class that will assign attributes at the bottom is Outer04
1
this
individual
yes
this
kind
set
righteousness
of
Outside
Department
class
name
+
1 this is the external class name defined in this way+
1 this is the external class name defined as + + 1
//
/*In fact, we have a class at the bottom to implement this method, but we can't find this class. This class can't be used once. We can't find this class and use it to create objects. You know, in fact, the system has optimized it for us
class Outer04$1 implements IA{
@Override
public void cry() {
System.out.println("tiger roaring...);
}
3. Member internal class 🙌
definition
The internal class of a member is defined in the member position of the external class, and there is no static modification. All members of the external class can be accessed directly, including private.
Code demonstration
class Outer01{ private int age=10; private String name="Zhang San"; //The inner class of the member is defined on the member of the outer class!!!!!! //The internal class of a member can be the same as that of a member. We can add the public private modifier to modify it. There is no problem class Inner{ public void say(){ //You can access all members of the external class, including private ones! System.out.println(age+name); } } }
How to use methods of inner classes?
We can define a function in the external class, instantiate the class, and then call it externally, because we can't operate outside the code block.
package day07; public class MemberInnerClass01 { public static void main(String[] args) { Outer01 outer01 = new Outer01(); outer01.hi();//Here we use it externally } } class Outer01{ private int age=10; private String name="Zhang San"; //The inner class of a member is defined on the member of the outer class!!!!!! //The internal class of a member can be the same as that of a member. We can add the public private modifier to modify it. There is no problem class Inner{ public void say(){ //You can access all members of the external class, including private ones! System.out.println(age+name); } } public void hi(){//The object is instantiated here Inner inner = new Inner(); inner.say();//Use member inner classes here } }
Scope
If we want to access the contents of this class, we will create a new object in a method of the external class, and then call the 1 method in this class.
Code display
class Outer01{ private int age=10; private String name="Zhang San"; //The inner class of a member is defined on the member of the outer class!!!!!! //The internal class of a member can be the same as that of a member. We can add the public private modifier to modify it. There is no problem class Inner{ public void say(){ //You can access all members of the external class, including private ones! System.out.println(age+name); } } public void hi(){//The object is instantiated here Inner inner = new Inner(); inner.say();//Use member inner classes here } }
Access rights
Members (properties and methods) of external classes can be accessed directly in internal classes
If the external class wants to access the internal class, we must first create an object of the internal class of the member, and then access it with the help of this object. (private properties can also be accessed in the same class)
How do other external classes access internal member classes
-
1. We first create an external class instance, and then create an internal class through the external class instance
-
2. We first create a method in the external class, which can return an instance of our internal class, and then we create it externally by calling this method (this creation is done in the external class!! note)
-
3. Create an internal class (new Outer()) without creating an external class new Inner()) / / this is the writing format
-
The following is a code demonstration of each creation process
-
Outer01.Inner inner = outer01.new Inner();//This is done through this object, and an internal object is created
Outer01.Inner inner1 = outer01.getInner();//The premise of this writing is that we create a method to return the instance object in the external class //This function needs to be written in an external class //This method returns an instance of Inner08 public Inner getInner(){ return new Inner();//Here we instantiate an object to receive externally, which is a bit like the get/set method }
3
Outer01.Inner inner2 = new Outer01().new Inner();//This is the same as the above. You can directly create an internal class without creating an external class instance
4. Static internal class 🍔
1. Definitions
The inner class of a member is defined in the member position of the outer class and is decorated with static. All members of the outer class can be accessed directly, including private.
Code demonstration
//Static inner class class Outer02{ private int age=10; private static String name="Li Si"; //1. Put it in the member position of the external class //2. Use the static modifier to access static members, but not non static members. At this time, the attributes of the static class!! This is actually the same as //The scope can also be used in the whole class body like other internal classes //Static internal class access external class can directly access all static members!!!! public static void hh(){ } static class Inner02{ private static String name="Wang Wu"; public void say(){ hh();//This is what you can access System.out.println(name); System.out.println(Outer02.name);//We don't need to add this keyword here, because we are static here. If we create it //It can be accessed directly through the class name, which is unnecessary } }
Scope
Because it is also an internal class, we can directly access the properties of the external class, but note that we need to access the static properties here. We have to access the static properties of the external class!!! (this should be distinguished from the internal category and department category of members)
How do external classes use static internal classes?
To create a method in an external class, create a new object in the method
public void setName(){//The way for external classes to access static internal classes is to create objects before accessing them Inner02 inner02 = new Inner02(); }
How do other external classes access static internal classes
1. (here, the static internal class can be directly regarded as a static member of the external class) so we can access it directly through the class name
//1. Method 1 because the static internal class can be accessed directly through the class name //This is different from the member inner class. The member inner class needs to create an object first (provided you follow the access permissions) Outer02.Inner02 inner02 = new Outer02.Inner02();//Because it is a static class, it can be accessed directly through the class name
2. Write a method to return the static class in the external class
//2. Method 2: write a non static method to return the object instance of the static internal class Outer02.Inner02 inner021 = new Outer02().getInner02(); public Inner02 getInner02(){ return new Inner02();//This is to get an instance object that returns this class }
3. Improvement method 2: we define the methods in the external class as static, so we can access them directly
//2. Method 3: write a non static method that can return the object instance of the static internal class Outer02.Inner02 inner021 = Outer02.getInner02(); public static Inner02 getInner02(){ return new Inner02();//This is to get an instance object that returns this class }
Summary 🎂
1. There are four internal classes
1. Local internal class
2. Anonymous inner class (key points must be mastered)
3. Member internal class
4. Static inner class (which is also a member inner class in essence)
2. Focus on mastering the use of anonymous inner classes
new interface / class (parameter list){
///... to rewrite some methods or something
};
Without him, only hands are familiar