• Java内部类(从入门到大成)


    活动地址:CSDN21天学习挑战赛

    一、什么是内部类

      将一个java类定义到另一个java类中的java类就是内部类。
      外部类—>包含内部类的类
      内部类—>外部类中的类
      注意:内部类编译后会形成一个新的字节码文件【外部类类名$内部类类名.class】

    例如:

    pubic  class  Hello{//外部类
    	public class  World{//内部类
    	}
    }
    
    • 1
    • 2
    • 3
    • 4

    二、成员内部类

      成员内部类—>类中方法外【成员变量】

    特征

    1. 成员内部类可以使用任意的访问限制修饰符。
    2. 成员内部类可以有实例变量、实例方法、构造方法,不能有静态元素。
    3. 成员内部类中的构造方法可以访问其他的构造方法【new】,可以访问实例变量/方法【对象.实例变量/方法 ,this.实例变量/方法,可以省略对象/this】。
    4. 成员内部类中的实例方法可以访问构造方法【new】,可以访问实例变量/方法【对象.实例变量/方法 ,this.实例变量/方法,可以省略对象/this】。
    5. 成员内部类中的构造方法可以访问外部类的构造方法,实例方法/变量,类方法/变量。
    6. 成员内部类中的实例方法可以访问外部类的构造方法,实例方法/变量,类方法/变量。
    7. 外内部类中的构造方法/实例方法可以访问成员内部类的构造方法,实例方法/变量,外内部类中的类方法不能访问成员内部类。
    8. 其他类中是可以访问成员内部类的,需要依赖外部类对象,注意访问限制修饰符。

    例如:

    package com.wangxing.test1;
    public class Hello {
    	public int helloid=1000;
    	public static String helloname="lisi";
    	public  Hello() {
    		World w=new World();
    		System.out.println("worldid=="+w.worldid);
    		w.worldMethod1();
    	}
    	public  Hello(int num) {}
    	public void  helloMehtod1() {
    		World w=new World();
    		System.out.println("worldid=="+w.worldid);
    		w.worldMethod1();
    	}
    	public void  helloMehtod2() {}
    	public static void  helloStaticMehtod1() {
    		//World w=new World();
    		//System.out.println("worldid=="+w.worldid);
    		//w.worldMethod1();
    	}
    	public static void  helloStaticMehtod2() {}
    	public class  World{
    		//成员内部类可以有实例变量
    		public int worldid=1001;
    		//成员内部类不可以有类变量
    		//public static String worldname="zhangsan";
    		//成员内部类可以有构造方法
    		//成员内部类中的构造方法可以访问其他的构造方法【new】,可以访问实例变量/方法【对象.实例变量/方法,this.实例变量/方法,可以省略对象/this】。
    		public World() {
    			World w=new World(100);
    			System.out.println("worldid=="+w.worldid);
    			System.out.println("worldid=="+this.worldid);
    			System.out.println("worldid=="+worldid);
    			w.worldMethod1();
    			this.worldMethod1();
    			worldMethod1();
    		}
    		//成员内部类中的构造方法访问外部类的
    		public World(int num) {
    			Hello  hello=new Hello(12);
    			System.out.println("helloid=="+hello.helloid);
    			System.out.println("helloid=="+Hello.this.helloid);
    			System.out.println("helloid=="+helloid);
    			hello.helloMehtod2();
    			Hello.this.helloMehtod2();
    			helloMehtod2();
    			System.out.println("helloname=="+hello.helloname);
    			System.out.println("helloname=="+Hello.this.helloname);
    			System.out.println("helloname=="+Hello.helloname);
    			System.out.println("helloname=="+helloname);
    			hello.helloStaticMehtod2();
    			Hello.this.helloStaticMehtod2();
    			Hello.helloStaticMehtod2();
    			helloStaticMehtod2();
    		}
    		//成员内部类可以有实例方法
    		//4.成员内部类中的实例方法可以访问构造方法【new】,可以访问实例变量/方法【对象.实例变量/方法,this.实例变量/方法,可以省略对象/this】。
    		public void worldMethod1() {
    			World w=new World(100);
    			System.out.println("worldid=="+w.worldid);
    			System.out.println("worldid=="+this.worldid);
    			System.out.println("worldid=="+worldid);
    			w.worldMethod1();
    			this.worldMethod1();
    			worldMethod1();
    		}
    		//成员内部类中的实例方法访问外部类
    		public void worldMethod2() {
    			Hello  hello=new Hello(12);
    			System.out.println("helloid=="+hello.helloid);
    			System.out.println("helloid=="+Hello.this.helloid);
    			System.out.println("helloid=="+helloid);
    			hello.helloMehtod2();
    			Hello.this.helloMehtod2();
    			helloMehtod2();
    			System.out.println("helloname=="+hello.helloname);
    			System.out.println("helloname=="+Hello.this.helloname);
    			System.out.println("helloname=="+Hello.helloname);
    			System.out.println("helloname=="+helloname);
    			hello.helloStaticMehtod2();
    			Hello.this.helloStaticMehtod2();
    			Hello.helloStaticMehtod2();
    			helloStaticMehtod2();
    		}
    		//成员内部类不可以有类方法
    		//public static void worldStaricMethod() {}
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    package com.wangxing.test1;
    public class Test1 {
    	public Test1() {
    		/*
    		//import com.wangxing.test1.Hello.World;
    		Hello  h=new Hello();
    		World w1=h.new World();
    		World w2=new Hello().new World();
            */
    		
    		Hello  h=new Hello();
    		Hello.World w1=h.new World();
    		Hello.World w2=new Hello().new World();
    	}
    	public  void myMethod() {
    		/*
    		//import com.wangxing.test1.Hello.World;
    		Hello  h=new Hello();
    		World w1=h.new World();
    		World w2=new Hello().new World();
            */
    		Hello  h=new Hello();
    		Hello.World w1=h.new World();
    		Hello.World w2=new Hello().new World();
    	}
    	public static void main(String[] args) {
    		/*
    		//import com.wangxing.test1.Hello.World;
    		Hello  h=new Hello();
    		World w1=h.new World();
    		World w2=new Hello().new World();
            */
    		Hello  h=new Hello();
    		Hello.World w1=h.new World();
    		Hello.World w2=new Hello().new World();
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    三、方法内部类

      方法内部类—>类中方法中【局部变量】

    特征

    1. 方法内部类不能使用任何访问限制修饰
    2. 方法内部类可以有实例变量/方法,构造方法,不能有静态元素。
    3. 方法内部类可以访问自己的实例变量/方法【对象/this,也可以省略】,构造方法
    4. 方法内部类可以访问本方法的局部变量【直接变量名称】
    5. 方法内部类可以访问外部类的构造方法,实例方法/变量,类方法/变量。
    6. 外部类是不能访问到方法内部类。

    例如:

    package com.wangxing.test1;
    
    public class Hello {
    	public  int helloid=1000;
    	public Hello() {}
    	public void  helloMehtod() {}
    	public static void  helloStaticMehtod() {}
    	
    	public void  helloMethod1(int num){
    		String name="zhangsan";
    		class  World{
    			public  int worldid=1001;
    			//public static  String worldname="zhangsan";
    			public World() {
    				World  w=new World(1);
    				System.out.println("worldid=="+w.worldid);
    				System.out.println("worldid=="+this.worldid);
    				System.out.println("worldid=="+worldid);
    				w.worldMethod2();
    				this.worldMethod2();
    				worldMethod2();
    				//4.方法内部类可以访问本方法的局部变量【直接变量名称】
    				System.out.println("name=="+name);
    				
    			}
    			public World(int a) {
    				Hello h=new Hello();
    				System.out.println("helloid=="+h.helloid);
    				System.out.println("helloid=="+Hello.this.helloid);
    				System.out.println("helloid=="+helloid);
    				h.helloMehtod();
    				Hello.this.helloMehtod();
    				helloMehtod();
    				h.helloStaticMehtod();
    				Hello.this.helloStaticMehtod();
    				Hello.helloStaticMehtod();
    				helloStaticMehtod();
    			}
    			public void worldMethod1() {
    				World  w=new World(1);
    				System.out.println("worldid=="+w.worldid);
    				System.out.println("worldid=="+this.worldid);
    				System.out.println("worldid=="+worldid);
    				w.worldMethod2();
    				this.worldMethod2();
    				worldMethod2();
    				//4.方法内部类可以访问本方法的局部变量【直接变量名称】
    				System.out.println("name=="+name);
    
    				Hello h=new Hello();
    				System.out.println("helloid=="+h.helloid);
    				System.out.println("helloid=="+Hello.this.helloid);
    				System.out.println("helloid=="+helloid);
    				h.helloMehtod();
    				Hello.this.helloMehtod();
    				helloMehtod();
    				h.helloStaticMehtod();
    				Hello.this.helloStaticMehtod();
    				Hello.helloStaticMehtod();
    				helloStaticMehtod();
    			}
    			public void worldMethod2() {}
    			//public static void worldStaticMethod() {}
    		}
    		
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

    四、静态嵌套类

      静态嵌套类—>成员内部类使用static修饰【类变量】

    特征

    1. 静态嵌套类中可以有构造方法,实例变量/方法,类变量/方法。
    2. 静态嵌套类中构造方法/实例方法可以访问本静态嵌套类中的构造方法,实例变量/方法,类变量/方法。
    3. 静态内部类中类方法可以访问本静态嵌套类中的构造方法,实例变量/方法【只能对象】,类变量/方法.
    4. 静态内部类中的构造方法/实例方法/类方法可以访问外部类的构造方法,实例变量/方法【只能对象】,类变量/方法。
    5. 静态嵌套类中不能有this.
    6. 外部类的构造方法/实例方法/类方法可以访问,静态内部类中构造方法,实例变量/方法【只能对象】,类变量/方法.
    7. 其他类中可以访问静态嵌套类【new 外部类类名.静态嵌套类名()】。注意访问限制修饰符

    例如:

    package com.wangxing.test1;
    
    public class Hello {
    	public int helloid=1000;
    	public static String helloname="lisi";
    	
    	public  Hello() {
    		World w=new World();
    		System.out.println("worldid=="+w.worldid);
    		w.worldMethod1();
    		System.out.println("worldname=="+w.worldname);
    		System.out.println("worldname=="+World.worldname);
    		w.worldStaricMethod1();
    		World.worldStaricMethod1();
    	}
    	public  Hello(int num) {
    		
    	}
    	
    	public void  helloMehtod1() {
    		World w=new World();
    		System.out.println("worldid=="+w.worldid);
    		w.worldMethod1();
    		System.out.println("worldname=="+w.worldname);
    		System.out.println("worldname=="+World.worldname);
    		w.worldStaricMethod1();
    		World.worldStaricMethod1();
    	}
    	public void  helloMehtod2() {
    		
    	}
    	
    	public static void  helloStaticMehtod1() {
    		World w=new World();
    		System.out.println("worldid=="+w.worldid);
    		w.worldMethod1();
    		System.out.println("worldname=="+w.worldname);
    		System.out.println("worldname=="+World.worldname);
    		w.worldStaricMethod1();
    		World.worldStaricMethod1();
    	}
    	public static void  helloStaticMehtod2() {
    		
    	}
    	
    	public static class  World{
    		//静态内部类可以有实例变量
    		public int worldid=1001;
    		//静态内部类可以有类变量
    		public static String worldname="zhangsan";
    		//静态内部类可以有构造方法
    		public World() {
    			World w=new World(100);
    			System.out.println("worldid=="+w.worldid);
    			System.out.println("worldid=="+this.worldid);
    			System.out.println("worldid=="+worldid);
    			w.worldMethod2();
    			this.worldMethod2();
    			worldMethod2();
    			System.out.println("worldname=="+w.worldname);
    			System.out.println("worldname=="+this.worldname);
    			System.out.println("worldname=="+World.worldname);
    			System.out.println("worldname=="+worldname);
    			w.worldStaricMethod2();
    			this.worldStaricMethod2();
    			World.worldStaricMethod2();
    			worldStaricMethod2();
    		}
    		public World(int num1) {
    			Hello  hello=new Hello(12);
    			System.out.println("helloid=="+hello.helloid);
    			hello.helloMehtod2();
    			System.out.println("helloname=="+hello.helloname);
    			System.out.println("helloname=="+Hello.helloname);
    			System.out.println("helloname=="+helloname);
    			hello.helloStaticMehtod2();
    			Hello.helloStaticMehtod2();
    			helloStaticMehtod2();
    		}
    		
    		//静态内部类可以有实例方法
    		public void worldMethod1() {
    			World w=new World(100);
    			System.out.println("worldid=="+w.worldid);
    			System.out.println("worldid=="+this.worldid);
    			System.out.println("worldid=="+worldid);
    			w.worldMethod2();
    			this.worldMethod2();
    			worldMethod2();
    			System.out.println("worldname=="+w.worldname);
    			System.out.println("worldname=="+this.worldname);
    			System.out.println("worldname=="+World.worldname);
    			System.out.println("worldname=="+worldname);
    			w.worldStaricMethod2();
    			this.worldStaricMethod2();
    			World.worldStaricMethod2();
    			worldStaricMethod2();
    		}
    		public void worldMethod2() {
    			Hello  hello=new Hello(12);
    			System.out.println("helloid=="+hello.helloid);
    			hello.helloMehtod2();
    			System.out.println("helloname=="+hello.helloname);
    			System.out.println("helloname=="+Hello.helloname);
    			System.out.println("helloname=="+helloname);
    			hello.helloStaticMehtod2();
    			Hello.helloStaticMehtod2();
    			helloStaticMehtod2();
    		}
    		//静态内部类可以有类方法
    		public static void worldStaricMethod1() {
    			World w=new World(100);
    			System.out.println("worldid=="+w.worldid);
    			w.worldMethod2();
    			System.out.println("worldname=="+w.worldname);
    			System.out.println("worldname=="+World.worldname);
    			System.out.println("worldname=="+worldname);
    			w.worldStaricMethod2();
    			World.worldStaricMethod2();
    			worldStaricMethod2();
    		}
    		public static void worldStaricMethod2() {
    			Hello  hello=new Hello(12);
    			System.out.println("helloid=="+hello.helloid);
    			hello.helloMehtod2();
    			System.out.println("helloname=="+hello.helloname);
    			System.out.println("helloname=="+Hello.helloname);
    			System.out.println("helloname=="+helloname);
    			hello.helloStaticMehtod2();
    			Hello.helloStaticMehtod2();
    			helloStaticMehtod2();
    		}
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    package com.wangxing.test1;
    
    public class Test1 {
    
    	public Test1() {
    		/*
    		//import com.wangxing.test1.Hello.World;
    		World w=new Hello.World();
    		w.worldMethod1();
    		w.worldStaricMethod1();
    		Hello.World.worldStaricMethod1();
    		*/
    		
    		Hello.World w2=new Hello.World();
    	}
    	
    	public  void myMethod() {
    		/*
    		//import com.wangxing.test1.Hello.World;
    		World w=new Hello.World();
    		w.worldMethod1();
    		w.worldStaricMethod1();
    		Hello.World.worldStaricMethod1();
    		*/
    		
    		Hello.World w2=new Hello.World();
    	}
    	public static void main(String[] args) {
    		/*
    		//import com.wangxing.test1.Hello.World;
    		World w=new Hello.World();
    		w.worldMethod1();
    		w.worldStaricMethod1();
    		Hello.World.worldStaricMethod1();
    		*/
    		
    		Hello.World w2=new Hello.World();
    		
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    五、内部类区分总结

    在这里插入图片描述

    六、匿名内部类

      匿名内部类—>【没有名字的内部类】就相当于某一个类/接口的子类,知识这个子类没有名字

    6.1继承式的匿名内部类

    package com.wangxing.test1;
    public class MyClass {
    	public  void  testMyClass() {
    		System.out.println("MyClass类的实例方法");
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    package com.wangxing.test1;
    public class Test1 {
    	public static void main(String[] args) {
    		MyClass mc=new MyClass() {
    			@Override
    			public void testMyClass() {
    			 System.out.println("重写MyClass类的testMyClass方法");	
    			}
    		};
    		mc.testMyClass();
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

      当一个类中的方法参数是抽象类类型时,我们可以传递上转型对象/子类对象,如果不想额外的创建一个子类,这时我们使用匿名内部类也是可以的

    package com.wangxing.test1;
    public abstract class TestClass {
    	public abstract void  testTClass();
    }
    
    • 1
    • 2
    • 3
    • 4
    package com.wangxing.test1;
    public class MyClass {
    	public  void  testMyClass2(TestClass  tc) {
    		tc.testTClass();
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    package com.wangxing.test1;
    public class Test1 {
    	public static void main(String[] args) {
    		MyClass mc=new MyClass();
    		mc.testMyClass2(new TestClass() {
    			@Override
    			public void testTClass() {
    				System.out.println("重写抽象类TestClass的抽象方法");
    			}
    		});	
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    运行结果:

    在这里插入图片描述

    6.2接口式的匿名内部类

    package com.wangxing.test2;
    public interface MyInterface {
    	void  testMethod();
    }
    
    • 1
    • 2
    • 3
    • 4
    package com.wangxing.test2;
    public class Test2 {
    	public static void main(String[] args) {
    		MyInterface  inter=new MyInterface() {
    			@Override
    			public void testMethod() {
    				System.out.println("重写接口的抽象方法");
    			}
    		};
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

      当一个类中的方法参数是接口类型时,我们可以传递接口回调对象/子类对象,如果不想额外的创建一个接口的子类,这时我们使用匿名内部类也是可以的。

    package com.wangxing.test2;
    public interface MyInterface {
    	void  testMethod();
    }
    
    • 1
    • 2
    • 3
    • 4
    package com.wangxing.test2;
    public class DoClass {
    	public  void  testDoMethod(MyInterface inter) {
    		inter.testMethod();
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    package com.wangxing.test2;
    public class Test2 {
    	public static void main(String[] args) {
    		DoClass dc=new DoClass();
    		dc.testDoMethod(new MyInterface() {
    			@Override
    			public void testMethod() {
    				System.out.println("重写接口的抽象方法");
    			}
    		});
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    运行结果:

    在这里插入图片描述

    总结

    在这里插入图片描述

  • 相关阅读:
    idea compile项目正常,启动项目的时候build失败,报“找不到符号”等问题
    java-php-python-ssm基于个人阅读习惯的个性化推荐系统研究计算机毕业设计
    Java零基础-正则表达式
    会声会影2024好不好用?有哪些新功能介绍
    如何用一行CSS实现10种现代布局
    【Flink 实战系列】Flink on yarn 为什么 Allocated CPU VCores 显示不正确?
    https转到http的nginx配置参考
    【MATLAB教程案例32】基于matlab的交通标志检测分割算法的仿真——形态学处理,膨胀,腐蚀,形状检测,颜色模型,小波滤波等知识的综合应用
    js双击修改元素内容并提交到后端封装实现
    输出司机和乘客重合的时间点
  • 原文地址:https://blog.csdn.net/qibulemingzi/article/details/126280422