JAVA Internal Class
1. What is an internal category?
The definition of one class is placed inside another class, and this class is called an internal class
2. What are the characteristics of internal classes?
1. The inner class is still an independent class. After compilation, the inner class will be compiled into an independent .class file, but it is preceded by the class name and $ symbol of the outer class.
2. Internal classes cannot be accessed in ordinary ways. The inner class is a member of the outer class, so the inner class can freely access the member variables of the outer class, whether private or not.
3. If the internal class is declared as static, it cannot access the member variables of the external class at will. At this time, the internal class can only access the static member variables of the external class.
3. What are the internal categories?
1. Member internal class
like:
package com.test01;public class A { // Inner class B inherits TestPojo and implements TestInterface interface class B extends TestPojo implements TestInterface{ // Inner class B's own method public void run(){ System.out.println("I'm running!"); } // Rewrite the interface method public void testf() { System.out.println("Implement the interface!"); } } // Call the internal class public void test(){ B b = new B() ; b.testf() ; // Use the rewrite interface method b.run() ; // Call your own method b.testpojo() ; // Call method inheriting the parent class} // Main method test public static void main(String[] args) { A a = new A() ; a.test() ; }}// Define an interface, the method is testf() interface TestInterface{ public void testf() ;}// Define a normal class method testpojo() class TestPojo{ public void testpojo() { System.out.println("I am a simple pojo class"); }}// Implement to call the methods in the inner class class Textone{ public static void main(String[] args) { AB b = new A().new B() ; //Calling the inner class B in class A /** is equivalent to the following code * A a = new A() ; * AB b = a.new B() ; * */ b.testf() ; //Use the rewrite interface method b.run() ; //Calling your own method b.testpojo() ; //Calling the method that inherits the parent class}}2. Method internal class
package com.test01;public class PerTest { public void test(){ // Define a method class Ne{ // Define a method internal class public void fle(){ // Define a method internal class System.out.println("I'm flying!"); } } ; new Ne().fle() ; //Calling the method of the inner class} public static void main(String[] args) { new PerTest().test() ; //Test}} Note: (1) The method internal class can only be instantiated within the method that defines the internal class, and cannot be instantiated outside this method.
(2) The method internal class object cannot use non-final local variables of the method where the inner class is located.
Because the local variables of the method are on the stack, they only exist during the life of the method. When a method ends, its stack structure is deleted.
Local variables become history. However, after the method is finished, the inner class objects created within the method may still exist in the heap!
For example, if a reference to it is passed to some other code and is stored in a member variable. Because local variables cannot be guaranteed to have the same long life as those of the method internal class objects, internal class objects cannot use them. (This understanding comes from Baidu Encyclopedia)
3. Anonymous internal class
1) Abstract anonymous internal classes
package com.anonymous;public class AbstractClass { public void test(){ //The method is test TestA a = new TestA(){ //Implement abstract class @Override public void run() { //Implement abstract class System.out.println("I am using abstract anonymous inner class"); } } ; a.run() ; //Calling the method of the inner class} public static void main(String[] args) { new AbstractClass().test() ; //Test}}//Define an abstract class TestA Abstract method is run()abstract class TestA{ public abstract void run() ;}2) Interface anonymous internal class
package com.anonymous;public class TestAnonymous { MyInterface m = new MyInterface(){ //Implement the interface public void eat() { //Rewrite the MyInterface interface method System.out.println("I'm having a meal!"); } } ; public void ss(){ //Method ss m.eat() ; //Call the overwritten method} public static void main(String[] args) { new TestAnonymous().ss() ; //Test}}//Define an interface method as eatinterface MyInterface{ public void eat() ;}Note: Anonymous inner classes can be defined within methods or in the members of the class. No matter which anonymous inner classes are, they cannot be called directly by external classes.
4. What is the role of internal categories?
Each inner class can independently inherit from an implementation (interface) so whether the peripheral class has inherited a certain implementation (interface) has no effect on the inner class. Without the ability provided by internal classes to inherit multiple concrete or abstract classes, some design and programming problems are difficult to solve. From this perspective, internal classes make the solution of multiple inheritance complete.
Interfaces solve some problems, while internal classes effectively implement "multiple inheritance".
Thank you for reading, I hope it can help you. Thank you for your support for this site!