Home >Java >javaTutorial >Java internal class instance analysis

Java internal class instance analysis

王林
王林forward
2023-05-04 15:07:161468browse

    Inner classes:

    In fact, as the name suggests, internal classes are classes within classes, and there is another class inside a class.

    Inner classes are divided into four types:

    1. Ordinary inner classes

    2. Static inner classes

    3. Methods Internal classes

    4. Anonymous internal classes

    Let’s learn about them one by one~~

    A. Ordinary internal classes:

    Let’s first understand it through the code:

    package InternalClass;
    /**
     * 内部类
     */
    public class Car {
        public int a = 10;
        public int b = 20;
        //外部类的方法
        public void method() {
            System.out.println("我是外部类Car的普通方法!");
            bus b = new bus();     //创建内部类的对象
            b.internalMethod();
        }
        //内部类(在Car类中的bus类)
        public class bus {
            int b = 30;
            //内部类方法
            public void internalMethod() {
                System.out.println("**这是bus内部类的普通方法!");
                System.out.println("**我是内部类,现在在访问外部类Car的a:" + a);
                System.out.println("**我是内部类,现在在访问内部类bus的b:" + this.b);
                System.out.println("**我是内部类,现在在访问外部类bus的b:" + Car.this.b);
                //注意,如果在内部类需要使用外部类的属性,必须通过外部的类去this外部类的属性
            }
        }    
    	//程序运行入口:
    	public static void main(String[] args) {
    	    Car car = new Car();   //创建外部类的对象
    	    car.method();
    	    // 运行上面的代码 会输出 : 我是外部类Car的普通方法!
    	   	//运行内部类的方法
    	   	//在上面method方法中已经创建了内部类bus的对象b
    	   	//这里就是通过使用外部类Car的对象car去new一个内部类对象
    	   	bus b = car.new bus();  
    	   	//通过内部类对象b 去使用方法即可
    	    b.internalMethod();   
    		//输出:
    		/*
    		**这是bus内部类的普通方法!
    		**我是内部类,现在在访问外部类Car的a:10
    		**我是内部类,现在在访问内部类bus的b:30
    		**我是内部类,现在在访问外部类bus的b:20
    */
       }
    }

    B. Static inner class:

    Static inner class, as the name implies, also modifies the inner class with static to make it static. You can call it directly with the name of the external class without creating an object:

    package InternalClass;
    public class Person {
        String name = "小王";
        static int age = 20;
        //创建静态内部类
        public static class Student{
            String name = "小红";
            //静态内部类的方法
            public static void study(){
                Person p = new Person(); //创建外部类的对象
                //静态访问非静态需要使用对象去调用
                System.out.println("内部类静态方法访问外部非静态成员:"+p.name);
                System.out.println("内部类静态方法访问外部静态成员:"+Person.age);
                System.out.println("内部类静态方法访问内部非静态成员:"+new Student().name);
            }
        }
        //程序运行入口
        public static void main(String[] args) {
            //静态内使用方法不需要创建对象
            Student.study();
        }
    /*
    输出:
    内部类静态方法访问外部非静态成员:小王
    内部类静态方法访问外部静态成员:20
    内部类静态方法访问内部非静态成员:小红
    */
    }

    C. Method internal class:

    Method The internal class, as the name suggests, is also a method in the external class. Create a new class in the body:

    package InternalClass;
    /**
     * 方法内部类
     */
    public class Student {
        //方法
        public void study(){
            int age = 20;
            String name = "小王";
            //在方法内写类:方法内部类
            class child{
                //方法内部类的方法
                public void play(){
                    System.out.println("孩子喜欢玩!");
                }
            }
            //在外部类study的方法内创建一个生成内部类child的对象,并使用方法
            child c = new child();
            c.play();
        }
        //程序执行入口
        public static void main(String[] args) {
            Student stu = new Student();
            //这里调用study方法后,在study方法体内创建了child的对象和调用了play的方法
            stu.study();  
        }
    }
    //输出:我叫:小王我今年:20我爱学习
    //	   孩子喜欢玩!

    Summary of internal classes (there are many limitations, so you should pay attention to usage scenarios):

    1. There cannot be access modifiers before the class

    2. Can only be used within this method

    3. Cannot create static information

    4. Local variables and parameters within the method can be directly accessed, but cannot be modified

    5. You can freely access any information of the external class

    D. Anonymous inner class:

    Anonymous inner class is an inner class without a name:

    First define an interface:

    public interface USB {
        void read();
    }
    package InternalClass;
    /**
     * 匿名内部类
     */
    public class Child {
        public static void main(String[] args) {
            USB u = new USB() {
                @Override
                public void read() {
                    System.out.println("这是匿名内部类,是USB接口没有具名的子类,必须重写 接口做具体实现");
                }
            };
            u.read();
        }
    }

    Four points to note about anonymous inner classes:

    1. Anonymous inner classes cannot define any static members. Method

    2. Methods in anonymous inner classes cannot be abstract

    3. Anonymous inner classes must implement all abstract methods of the interface or abstract parent class

    4.Anonymous External class member variables or member methods accessed by internal classes must be modified with static

    Summary of internal classes:

    1. Why use internal classes?

    Internal classes have the basic characteristics of classes: they can inherit parent classes and implement interfaces. In actual problems, we will encounter some problems that cannot be solved by interfaces. In this case, we can use internal classes to inherit a specific or Abstract classes indirectly solve a series of problems caused by the inability of multiple inheritance classes.

    2. Benefits of inner classes:

    Inner classes can use multiple instances. Each instance has its own status information and is independent of the information of other peripheral objects. Internal classes do not The confusing "is-a" relationship, it is an independent entity. The inner class provides better encapsulation. Except for the outer class, other classes cannot access the inner class object. The moment when the inner class object is created does not depend on the outer class object. create

    The above is the detailed content of Java internal class instance analysis. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete