Introduction to Java (18) - Internal Classes

Introduction to Java (18) - Internal Classes

Table of contents

Introduction to Java (18) - Internal Classes

inner class

Non-static member inner class

static inner class

anonymous inner class

Sample code

 


inner class

      - Define a new class inside the class

      * Classification:
           - Static inner classes: similar to static variables
           - Instance inner classes: similar to instance variables
           - Local inner classes: similar to local variables
      - Code written by inner classes has poor readability and should be used as little as possible
      - Anonymous inner classes are inner classes kind of

Non-static member inner class

           - As a member of a class, you can use 4 permission modifiers to modify it. Compared with external classes, external classes can only use two permission modifiers (public and default) to modify.
           - Non-static member inner classes can have their own member variables, constructors, and member functions. But there cannot be static methods. From a time perspective, static method creation means that the inner class has not been initialized.
           - In the methods of non-static members of the inner class, you can directly access the members of the outer class: member variables, member methods, and constructors.


- The outer class cannot directly access the members of the inner class. You must first create an inner class object and use the object name to access the inner class members. OutClass.IntClass a=new OutClass.new IntClass
- The prerequisite for creating an inner class object is to create an object of the outer class first
- the static method of the outer class cannot access the non-static inner class, including the inability to create an instance of the inner class, so it cannot Access member methods and member variables of inner classes.

static inner class

           - Static members Inner classes, like other static members, belong to the class.
           - Static members Inner classes can have static methods.
           - Static members Inner classes (static methods and non-static methods) can only access static members of the outer class.
           - Static members In the method of the inner class, the static members of the outer class can be directly called through the class name of the outer class, or through the object name.
           - In the Test class, inner class objects can be created directly through the class name.

anonymous inner class

      - Indicates that this class has no name, cannot be reused, the code is too messy, and the readability is too poor
      - See examples for usage methods

Sample code


public class InnerClass{
	public static void main(String[] args) {
		Math sum1=new Math();
		//sum1.sum(???, 100, 200);//无法直接使用引用数据类型
		
		
		//一般情况
		Computerout c=new Computerout();//对接口的实现类进行建立对象
		sum1.sum(c, 100, 200);//对象的调用
		
		//内部类
		sum1.sum(new Computer(){//new Computer(){}相当于接口的实现类,只是在方法的调用上将方法具体
			public int sum(int a,int b) {
				int sum=0;
				sum=a+b;
				return sum;
			}
		}, 100, 200);
		
	}
}


class Computerout implements Computer{//Computer接口的实现类
	public int sum(int a,int b) {
		int sum=0;
		sum=a+b;
		return sum;
	}
}


interface Computer{//接口
	int sum(int a,int b);
}
class Math{//基本外部类实现
	public void sum(Computer c,int x,int y) {
		int sum=c.sum(x, y);
		System.out.println("实验结果是:"+sum);
	}
}


 

Guess you like

Origin blog.csdn.net/qq_61562251/article/details/135047178