Internal class:
A class is nested inside another class, so that the inner class can share some resources with the outer class
Internal classes are divided into four forms:
Member inner class
Static inner class
Local inner class
Anonymous Inner Class
Member inner class
In a class, you can define member methods and member variables. In addition, you can also define member internal classes
//External class public class MemberOuterClass{ //Properties of external classes private String name; private static int age; //Methods of external classes public void run(){} public static void go(){} /* Member inner class declaration start */ public class MemberInnerClass{ private String name; private int age; public void run(String name){} } /* End of member inner class declaration */ }
Note that static properties and methods cannot be written in member internal classes
Note that after the current code is compiled successfully, two class files will be generated, one corresponding to the external class and one corresponding to the internal class. The names of the two class files generated by compilation are respectively:
MemberOuterClass.class
MemberOuterClass$MemberInnerClass.class
Mutual access between internal and external classes of members:
-
Member internal classes access external properties and methods
public class MemberOuterClass { //Properties of external classes private String name; private static int age; //Methods of external classes public void run(){} public static void go(){} /* Member inner class declaration start */ public class MemberInnerClass{ private String name; private int age; public void run(String name){ //Access the parameter name in the current run method System.out.println(name); //Access the internal class's own attribute name System.out.println(this.name); //Accessing non static properties of an external class System.out.println(MemberOuterClass.this.name); //Accessing static properties of an external class System.out.println(MemberOuterClass.age); //Accessing non static methods of external classes MemberOuterClass.this.run(); //Accessing static methods of external classes MemberOuterClass.go(); } } /* End of member inner class declaration */ }
If the inner class uses the instance property of the outer class: the outer class this. External class property name
int name = MemeberOuter.this.name; int age = MemeberOuter.age;
-
The external class accesses the properties and methods of the member's internal class
public class MemberOuterClass { //Methods of external classes to access properties and methods of internal classes of members public void test(){ //You need to create an internal class object before you can access it MemberInnerClass t = new MemberInnerClass(); System.out.println(t.name); System.out.println(t.age); t.run("tom"); } /* Member inner class declaration start */ public class MemberInnerClass{ private String name; private int age; public void run(String name){ } } /* End of member inner class declaration */ }
Use this inner class in other classes (create objects):
If the inner class of the member is not private decorated, the inner class can be accessed in other classes
public class Test { public static void main(String[] args) { MemberOuterClass moc = new MemberOuterClass(); MemberInnerClass mic = moc.new MemberInnerClass(); mic.run("tom"); } }
In other classes, when using this non private decorated member inner class, you should pay attention to the following points:
- This inner class needs import and is an outer class Import as an internal class.
- When creating objects, you need to create external class objects first, and then use external class objects to create internal class objects.
The form is: external class object new inner class object ();
Can an interface be nested inside a class in addition to another class?
Yes, not only interfaces can be nested in the class, but also other interfaces can be nested inside the interface.
For example, refer to Java util. Internal interface Entry in map interface
Under what circumstances will internal classes be used?
When abstracting things, if there are other things inside a thing, you can consider using the structure of internal class.
For example, a Car contains an Engine. In this case, the Engine class can consider (but not necessarily) using an internal class to describe the member positions defined in the Car class.
This design can not only express the close relationship between Car and Engine, but also easily use the attributes and methods in Car in the Engine class
Static inner class
The static inner class is similar to the member inner class, except that the inner class is decorated with the static keyword.
Note that in the static inner class, you can write static attributes and methods. In addition, among the four inner classes, only the static inner class can write static attributes and methods
Mutual access between static internal and external classes:
-
Static internal classes access external properties and methods
public class StaticOuterClass { //Properties of external classes private String name; private static int age; //Methods of external classes public void run(){} public static void go(){} /* Start of static inner class declaration */ public static class StaticInnerClass{ private String name; private static int age; public void run(String name){ //Access the parameter name in the current run method System.out.println(name); //Access the internal class's own attribute name System.out.println(this.name); //Access the static properties of the inner class System.out.println(age); //Non static properties and methods of external classes cannot be accessed in static internal classes //System.out.println(StaticOuterClass.this.name); //StaticOuterClass.this.run(); //Accessing static properties and methods of external classes System.out.println(StaticOuterClass.age); StaticOuterClass.go(); } } /* End of static inner class declaration */ }
Note that non static properties and methods in the external class cannot be accessed in the static internal class. If you want to use them, you can only create objects first
-
External classes access the properties and methods of static internal classes
public class StaticOuterClass { public void test(){ //In the external class, access the static properties in the static class System.out.println(StaticInnerClass.age); //In the external class, access the non static properties and methods in the static internal class StaticInnerClass sic = new StaticInnerClass(); System.out.println(sic.name); sic.run("tom"); } /* Start of static inner class declaration */ public static class StaticInnerClass{ private String name; private static int age; public void run(String name){ } } /* End of static inner class declaration */ }
Note that non static properties and methods in the internal class cannot be accessed in the static external class. If you want to use them, you can only create objects first
You can import using static import
Local internal class (internal definition, internal use)
Local internal class is another form of internal class. In the method declared in the external class, it is equivalent to the position of the local variable in the method. Its scope of action is only in the current method.
Local inner classes are the least commonly used inner classes.
Mutual access of local internal and external classes:
- Local internal classes access external properties and methods
public class LocalOuterClass { //Properties of external classes private String name; private static int age; //Methods of external classes public void run(){} public static void go(){} public void sayHello(String name){ /* Start of local inner class declaration */ class LocalInnerClass{ private String name; public void test(String name){ //Access the parameter name in the current test method System.out.println(name); //Access the internal class's own attribute name System.out.println(this.name); /*Note that the parameter name of the sayHello method cannot be accessed because there is no way to express it. Replace it with another one After the name, you can access it. Don't just call name*/ //Accessing non static properties of an external class System.out.println(LocalOuterClass.this.name); //Accessing non static methods of external classes LocalOuterClass.this.run(); //Accessing static properties and methods of external classes System.out.println(LocalOuterClass.age); LocalOuterClass.go(); } } /* End of local inner class declaration */ } }
- In the local internal class, access the variable in the current method, which must be final modified
public void sayHello(final String name){ final int num = 1; /* Start of local inner class declaration */ class LocalInnerClass{ public void test(){ System.out.println(name); System.out.println(num); //An error is reported during compilation. The variable modified by final can only be assigned once //name = "tom"; //num = 2; } } /* End of local inner class declaration */ }
At jdk1 8. If a local variable is used in a local inner class, the local variable will automatically become final
- External classes access properties and methods of local internal classes
public void sayHello(String name){ /* Start of local inner class declaration */ class LocalInnerClass{ private int num; public void test(){ } } /* End of local inner class declaration */ //Create a local internal class object LocalInnerClass lic = new LocalInnerClass(); //Object access properties System.out.println(lic.num); //Object call method lic.test(); }
Local inner class, which can only be used in the currently declared method.
Anonymous inner class (the implementation of the interface will take a while, so there is no need to define a class to implement, just use anonymous inner class)
Anonymous inner class is an inner class without name. It is a simplified writing of inner class. In later code, the anonymous inner class is
The most used inner class. (very important)
In normal code, the steps of using an interface are as follows:
Declare a class to implement this interface
Implement the abstract method in this interface (override)
In other code, create an object of this class
Call the method implemented (overridden) in the class
In fact, in this process, our goal is to implement (rewrite) the abstract methods in the interface, and finally call the implementation
Post (rewrite) method.
Then, by using anonymous inner classes, we can simplify this process and make it easier for us to call methods to the party after implementation (Rewriting)
Law!
Format:
Parent class or interface type variable name = new Parent class or interface(){ // Method rewrite @Override public void method() { // Execute statement } }; //Call the implemented (overridden) method Variable name.method();
There are two forms of anonymous inner classes:
Use a parent class to declare and create an anonymous inner class object. The anonymous inner class is the subtype of the parent class by default
Use an interface to declare and create anonymous inner class objects. The anonymous inner class is the implementation class of the interface by default
Anonymous inner class because there is no class name:
Anonymous inner classes must rely on a parent type or an interface
When an anonymous inner class is declared, it must create an object, otherwise it cannot be created later
Constructors cannot be defined in anonymous inner classes
For example, use a parent type to declare and create an anonymous inner class object
public abstract class Animal { public abstract void run(); } class Test{ public static void main(String[] args) { Animal animal = new Animal(){ @Override public void run() { System.out.println("Default implementation in anonymous inner classes"); } }; animal.run(); } }
Note that if the anonymous inner class is declared with the parent type, the anonymous inner class is the subclass of the parent type by default
For example, use interfaces to declare and create anonymous inner class objects
public interface Action { void run(); } class Test{ public static void main(String[] args) { Action a = new Action(){ @Override public void run() { System.out.println("Default implementation in anonymous inner classes"); } }; a.run(); } }
Note that if the anonymous inner class is declared by the interface, the anonymous inner class is the implementation class of the interface by default
Selection of internal classes:
Assuming that you have determined that you want to use internal classes, how do you choose them in general?
- Consider this inner class. If it needs to be used repeatedly (it must have a name)
In this inner class, if you need to define static properties and methods, choose to use the static inner class
In this internal class, if you need to access the non static properties and methods of the external class, choose to use the member internal class - Consider this inner class. If you only need to use it once (you can have no name)
Choose to use anonymous inner classes - Local internal classes, rarely used
Packing class: the corresponding class type provided for the eight basic types
Basic type packing type
boolean java.lang.Boolean
byte java.lang.Byte
short java.lang.Short
int java.lang.Integer
long java.lang.Long
float java.lang.Float
double java.lang.Double
char java.lang.Character
Automatic packing / unpacking
JDK1.5 or above, it can support automatic packing and unpacking between basic types and packaging types
//JKD1. Before 5 Integer o = new Integer(1); Integer o = Integer.valueOf(1); //JDK1. After 5 //Auto boxing, where the number 1 is automatically wrapped into an Integer type object Integer o = 1; //JKD1. Before 5 Integer o = new Integer(1); int i = o.intValue(); //JDK1. After 5 Integer o = new Integer(1); //Automatic unpacking. Here, the object o will be unpacked into an int type number automatically, and the number will be assigned to the int type variable i int i = o;
Object common methods
1 toString method
This method can return the default string form of an object:
return getClass().getName() + "@" + Integer.toHexString(hashCode());
2 getClass method
This method is a very important method. It can return the object pointed to by a reference at run time. What is the specific type
This method is a native modified local method, not implemented in java language.
3 equals method
This method can compare whether two objects are equal
The equals method in Object directly uses the = = sign to compare whether the address values of two objects are equal (rewriting can meet the requirements)
When rewriting the equals method, you should pay attention to the following points:
- Reflexivity: any reference to obj, obj The return value of equals (obj) must be true
- Symmetry: for any reference o1, o2, if and only if o1 When the return value of equals (o2) is true, o2 The return value of equals (o1) is one
Set to true; - Transitivity: if O1 Equals (O2) is true, O2 If equals (O3) is true, O1 Equals (O3) must also be true
- Consistency: if the objects involved in the comparison have not changed, the results of the object comparison should not have changed
- Nonempty: any nonempty reference obj, obj The return value of equals (null) must be false
4 hashCode method
This method returns an int value, which is a result calculated by the JVM through the hash algorithm according to the characteristics (address value) of the object in memory.
public native int hashCode();
For hashCode values of two objects:
For two objects that are equal, the hashCode value must be equal
If the hashCode value is the same, the two objects may be equal or different
If the hashCode value is different, the two objects must be different
String
String is the most used data in the program. The JVM has specially set up an area (string constant pool) in memory to improve the use efficiency of word string objects.