16.Java中的内部类

1.什么是内部类?

将一个java类定义到另一个类的类体中,那么定义在类体中的类就是内部类
我们把包含内部类的java类叫外部类

public  class  Hello{

	public  class  World{

	}

}

 Hello---外部类
World---内部类
内部类在编译之后会得到独立的内部类的字节码文件【外部类的类名$内部类的类名.class】

2.内部类的分类?

        1.成员内部类
        2.方法内部类
        3.匿名内部类
        4.静态内部类【静态嵌套类】

3.每一个种内部类的编写形式,以及其各自的特点

        3.1 成员内部类
            将内部类定义在另一个类的类体中

HEllo--外部类
public class Hello{
Word--内部类
	public class Word{
		
	}
}

                      成员内部类中的元素:实例变量,构造方法,实例方法;不能出现静态元素

public class Test1 {
	//成员内部类
	public class Test11{
		public String t11shili="Test11中的实例变量";
		//不能使用静态实例变量
//		public static String t11jingtai = "成员内部类中不能出现静态变量";
		public Test11(){
			System.out.println("成员内部类中的构造方法");
		}
		public void t11shiliff(){
			System.out.println("成员内部类中的实例方法");
		}
		//成员内部类不能出现静态方法
//		public static void t11jingtaiff(){}
		
	}
}

    成员内部类中访问成员内部类中的元素
        方法可以访问成员内部类中的实例变量/实例方法,构造方法
        1.对实例变量/实例方法,对象.实例变量/实例方法,this.实例变量/实例方法,对象和this可以省略
        2.对构造方法,通过new访问。 

package zx.javatest.a;

public class Test2 {
	public class Test22{
		public String t22shili="成员内部类中的实例变量";
		public Test22(){
			System.out.println("成员内部类中的构造方法");
		}
		public Test22(String name){
			Test22 t2 = new Test22();
			System.out.println(t2.t22shili);
			System.out.println(this.t22shili);
			System.out.println(t22shili);
			t2.t22shili();
			this.t22shili();
			t22shili();
		}
		public void t22shili(){
			System.out.println("成员内部类中的实例方法");
		}
		
		public void t22shili2(){
			System.out.println("成员内部类中的实例方法");
			Test22 t2 = new Test22();
			System.out.println(t2.t22shili);
			System.out.println(this.t22shili);
			System.out.println(t22shili);
			t2.t22shili();
			this.t22shili();
			t22shili();
		}

	}
}

成员内部类中访问外部类中的元素
        方法中可以访问外部类的实例变量/实例方法,静态变量/静态方法,构造方法
        1.对实例变量/实例方法的访问,对象.实例变量/实例方法,外部类名.this.实例变量/实例方法,对象/外部类名.this可以省略。
        2.静态变量/静态方法的访问,对象.静态变量/静态方法,外部类名.this.静态变量/静态方法,外部类类名.静态变量/静态方法,对象/外部类名.this/外部类类名可以省略。        
        3.构造方法访问,通过new访问

package zx.javatest.a;

public class Test3 {
	public  String helloshili="外部类中的实例变量";
	public static  String hellostatic="外部类中的静态变量";
	public  Test3(){
		System.out.println("外部类的构造方法");
	}
	public  void  helloShiLi(){
		System.out.println("外部类的实例方法");
	}
	public static void  helloStatic(){
		System.out.println("外部类的静态方法");
	}
	public  class  World3{
		public World3(){
			Test3 h3=new Test3();
			System.out.println(h3.helloshili);
			System.out.println(Test3.this.helloshili);
			System.out.println(helloshili);
			System.out.println(h3.hellostatic);
			System.out.println(Test3.this.hellostatic);
			System.out.println(Test3.hellostatic);
			System.out.println(hellostatic);
			h3.helloShiLi();
			Test3.this.helloShiLi();
			helloShiLi();
			h3.helloStatic();
			Test3.this.helloStatic();
			Test3.helloStatic();
			helloStatic();
		}
		public  void  worldShiLiMethod1(){
			Test3 h3=new Test3();
			System.out.println(h3.helloshili);
			System.out.println(Test3.this.helloshili);
			System.out.println(helloshili);
			System.out.println(h3.hellostatic);
			System.out.println(Test3.this.hellostatic);
			System.out.println(Test3.hellostatic);
			System.out.println(hellostatic);
			h3.helloShiLi();
			Test3.this.helloShiLi();
			helloShiLi();
			h3.helloStatic();
			Test3.this.helloStatic();
			Test3.helloStatic();
			helloStatic();
		}
	}
}

 
    外部类中访问成员内部类中的元素
        在外部类中的构造方法/实例方法中可以访问成员内部类中的实例变量/实例方法,构造方法,只能对象访问,不能省略。
        在外部类中的静态方法中不能访问成员内部类中的元素。

package zx.javatest.a;

public class Test4 {
	public  String helloshili="外部类中的实例变量";
	public static  String hellostatic="外部类中的静态变量";
	public  Test4(){
		System.out.println("外部类的构造方法");
		World4 w4=new World4();
		System.out.println(w4.worldshili);
		w4.worldShiLiMethod1();
	}
	public  void  helloShiLi(){
		System.out.println("外部类的实例方法");
		World4 w4=new World4();
		System.out.println(w4.worldshili);
		w4.worldShiLiMethod1();
	}
	public static void  helloStatic(){
		System.out.println("外部类的静态方法");
		//World4 w4=new World4();
		//System.out.println(w4.worldshili);
		//w4.worldShiLiMethod1();
	}
	//成员内部类
	public  class  World4{
		public String  worldshili="成员内部类中的实例变量";
		public World4(){
			System.out.println("成员内部类中的构造方法");
		}
		public  void  worldShiLiMethod1(){
			System.out.println("成员内部类中的实例方法");
		}
	}
}

   其他类中访问成员内部中的元素
          1.外部类名.内部类名 w1=new 外部类构造方法().new 内部类构造方法();

          2.外部类名 外部类对象=new 外部类构造方法();
                外部类名.内部类名 w1=外部类对象.new 内部类构造方法();

          3.import  包名.外部类名.内部类名;
            3.1 内部类名 w1=new 外部类构造方法().new 内部类构造方法();
            3.2 外部类名 外部类对象=new 外部类构造方法();
            内部类名 w1=外部类对象.new 内部类构造方法();

package com.wangxing.chengyuan.test1;

public class Hello {
	//成员内部类
	public  class  World{
		public String  worldshili="成员内部类中的实例变量";
		public World(){
			System.out.println("成员内部类中的构造方法");
		}
		public  void  worldShiLiMethod(){
			System.out.println("成员内部类中的实例方法");
		}
	}
}

package com.wangxing.chengyuan.test1;
import com.wangxing.chengyuan.test1.Hello.World;
public class Other {
	public Other(){
		Hello.World w1=new Hello().new World();
		System.out.println(w1.worldshili);
		w1.worldShiLiMethod();
	}
	public void shili(){
		Hello  hello=new Hello();
		Hello.World w1=hello.new World();
		System.out.println(w1.worldshili);
		w1.worldShiLiMethod();
	}
	public static void staticmethod(){
		Hello  hello=new Hello();
		World w1=hello.new World();
		System.out.println(w1.worldshili);
		w1.worldShiLiMethod();
	}
}

 3.2.方法内部类---就类似于类中的局部变量

将一个java类定义在另一个类的方法中
    1.我们可以在一个java类中的任意一个方法中定义方法内部类
    2.定义在方法中的方法内部类不能使用任何访问限制修饰符

package zx.javatest.a;
//内部类
//方法内部类---就类似于类中的局部变量
public class Test1 {
	public Test1 (){
		//构造方法中的方法内部类
		class Test11{
			
		}
	}
	public void Test2(){
		//实例方法中的方法内部类
		class Test22{
			
		}
	}
	public static void Test3(){
		//静态方法中的方法内部类
		class Test33{
			
		}
	}
}

方法内部类中可以定义实例变量/实例方法,构造方法,不能有静态变量/静态方法。

package zx.javatest.a;
//方法内部类中可以定义实例变量/实例方法,构造方法,
//不能有静态变量,静态方法

public class Test2 {
	public void shili(){
		class Nei{
			public String neishili="方法内部类的实例变量";
			//方法内部类中不能有静态变量
			//public static String neijingtai="方法内部类的静态变量";
			public Nei(){
				System.out.println("方法内部类的构造方法");
			}
			public void neislff(){
				System.out.println("方法内部类的实例方法");
			}
			//方法内部类中不能有静态方法
			//public static neijtff(){}
		}
	}
}

 方法内部类中可以访问方法内部类中的元素
        1.对构造方法的访问,通过new方法
        2.对实例变量/实例方法的访问,this.实例变量/实例方法,对象.实例变量/实例方法,this和对象可以省略

package zx.javatest.a;
//方法内部类中可以访问方法内部类中的元素
public class Test3 {
	//1.对构造方法的访问,通过new方法
	//2.对实例变量/实例方法的访问,this.实例变量/实例方法,对象.实例变量/实例方法,this和对象可以省略
	public void shili(){
		class Nei{
			public String neishili="方法内部类的实例变量";
			public Nei(){
				System.out.println("方法内部类的无参构造方法");
			}
			public Nei(String name){
				System.out.println("方法内部类的有参构造方法");
				Nei n1 = new Nei();
				System.out.println(this.neishili);
				System.out.println(n1.neishili);
				System.out.println(neishili);
				this.neislff1();
				n1.neislff1();
				neislff1();
			}
			public void neislff1(){
				System.out.println("方法内部类的实例方法");
			}
			public void neislff2(){
				System.out.println("方法内部类的实例方法");
				Nei n1 = new Nei();
				System.out.println(this.neishili);
				System.out.println(n1.neishili);
				System.out.println(neishili);
				this.neislff1();
				n1.neislff1();
				neislff1();
			}
			
		}
	}
}

        方法内部类中可以访问定义该方法内部类的方法里的局部变量,方法里的局部变量默认final修饰符修饰,为常量,可以直接变量名称访问。


        方法内部类中可以访问外部类中的元素
        1.对构造方法的访问,通过new访问
        2.对实例元素的访问,对象.实例元素,外部类类名.this.实例元素,对象/外部类类名.this可以省略。
        3.对静态元素的访问,对象.静态元素,外部类类名.this.静态元素,外部类类名.静态元素,对象/外部类类名.this/外部类类名可以省略。

public class Test5 {
	public String waishili = "外部类的实例变量";
	public static String waistatic = "外部类的静态变量";
	public Test5(){
		System.out.println("外部类的构造方法");
	}
	public void waishili(){
		System.out.println("外部类的实例方法");
	}
	public static void waijingtai(){
		System.out.println("外部类的静态方法");
	}
	
	public void shili(){
		String jububianliang="局部变量";
		class World{
			public  String  worldshili="方法内部类的实例变量";
			public World(){
				System.out.println("方法内部类的构造方法");
				//1.对构造方法的访问,通过new访问
				Test5 h4=new Test5();
				//2.对实例元素的访问,对象.实例元素,外部类类名.this.实例元素,
				//对象/外部类类名.this可以省略。
				System.out.println(h4.waishili);
				System.out.println(Test5.this.waishili);
				System.out.println(waishili);
				h4.waishili();
				Test5.this.waishili();
				waishili();
				//3.对静态元素的访问,对象.静态元素,外部类类名.this.静态元素,外部类类名.静态元素,
				//对象/外部类类名.this/外部类类名可以省略。
				System.out.println(h4.waistatic);
				System.out.println(Test5.this.waistatic);
				System.out.println(Test5.waistatic);
				System.out.println(waistatic);
				h4.waijingtai();
				Test5.this.waijingtai();
				Test5.waijingtai();
				waijingtai();
			}
			public void wordMethod1(){
				System.out.println("方法内部类的实例方法");
				//1.对构造方法的访问,通过new访问
				Test5 h4=new Test5();
				
				System.out.println(h4.waishili);
				System.out.println(Test5.this.waishili);
				System.out.println(waishili);
				h4.waishili();
				Test5.this.waishili();
				waishili();
				
				
				System.out.println(h4.waistatic);
				System.out.println(Test5.this.waistatic);
				System.out.println(Test5.waistatic);
				System.out.println(waistatic);
				h4.waijingtai();
				Test5.this.waijingtai();
				Test5.waijingtai();
				waijingtai();
			}
		}
	}

 定义方法内部类的方法中,可以访问方法内部类的元素
    1.对构造方法的访问,通过new
    2.对实例元素的访问,只能对象访问

package zx.javatest.a;
//定义方法内部类的方法中,可以访问方法内部类的元素
public class Test6 {
	public  void  shiliMethod(){
		class  World{
			public  String  worldshili="方法内部类的实例变量";
			public World(){
				System.out.println("方法内部类的构造方法");
			}
			public  void  wordMethod1(){
				System.out.println("方法内部类的实例方法");
			}
		}
		//访问方法内部类中的构造方法,new
		World  w=new World();
		//只能对象访问,实例变量和实例方法
		System.out.println(w.worldshili);
		w.wordMethod1();
	}
//外部类中不能访问方法内部类中的元素,方法内部类就相当于是方法中的一个局部变量
}

3.3 匿名内部类----就相当于是子类

没有名字内部类

那个java元素的{}中既可以定义变量,也可以定义方法?
java类中既可以定义变量,也可以定义方法
由此可知下面的代码是在创建一个java类

{
 public  String  shili="实例变量";
 public  void  shiliMehtod(){
	System.out.println("MyClass类的实例方法111");
	}
}

只是没有具体的类名称,而且是写在了另一个类中,所以它是一个内部类
我们把它叫匿名内部类

new MyClass(){
        public  String  shili="实例变量";
	public  void  shiliMehtod(){
		System.out.println("MyClass类的实例方法111");
	}
}

上面的代码是在创建匿名内部类的对象.创建出来的匿名内部类的对象就是MyClass类的子类对象
由此可知上面这个代码是在指明创建一个MyClass类的子类。

匿名内部类本质是一个子类

new MyClass(){
	public  String  shili="实例变量";
	//方法重写
	public  void  shiliMehtod(){
		System.out.println("MyClass类的实例方法111");
	}
};
new--新建对象
MyClass()---MyClass类的子类
{
	public  String  shili="实例变量";
	//方法重写
	public  void  shiliMehtod(){
		System.out.println("MyClass类的实例方法111");
	}
}---匿名内部类
新建MyClass类的子类对象


 匿名内部类本质是一个子类
优点:是减少类的创建
缺点:看不懂

3.4静态内部类【静态嵌套类】--- 就相当于静态变量

就是给成员内部类上添加static修饰符

public class Hello {
    public static class World{
        
    }
}

静态内部类可以出现构造方法,实例元素,静态元素

package zx.javatest.c;
//静态内部类
public class Test1 {
	//就是给成员内部类上添加static修饰符
	public static class Test11{
		//静态内部类可以出现构造方法,实例元素,静态元素
		public  String worldshili="静态内部类中的实例变量";
		public static String worldstatic="静态内部类中的静态变量";
		public Test11(){}
		public  void worldshiliMethod(){}
		public static void worldstaticMethod(){}
	}
}

静态内部类中访问静态内部类的元素
1.在静态内部类的构造方法和实例方法中可以访问静态内部类中构造方法,实例元素,静态元素
    构造方法--new
    实例元素--对象/this,可以省略
    静态元素--对象/this/类名,可以省略
2.在静态内部类的静态方法中可以访问静态内部类中构造方法,实例元素,静态元素
    构造方法--new
    实例元素--对象
    静态元素--对象/类名,可以省略
    不能使用this

public static class World1{
		public  String worldshili="静态内部类中的实例变量";
		public static String worldstatic="静态内部类中的静态变量";
		public World1(){}
		public World1(String  name){
			World1 w11=new World1();
			
			System.out.println(w11.worldshili);
			System.out.println(this.worldshili);
			System.out.println(worldshili);
			w11.worldshiliMethod1();
			this.worldshiliMethod1();
			worldshiliMethod1();
			
			System.out.println(w11.worldstatic);
			System.out.println(this.worldstatic);
			System.out.println(World1.worldstatic);
			System.out.println(worldstatic);
			w11.worldstaticMethod1();
			this.worldstaticMethod1();
			World1.worldstaticMethod1();
			worldstaticMethod1();
		}
		public  void worldshiliMethod1(){}
		public  void worldshiliMethod2(){
			World1 w11=new World1();
			
			System.out.println(w11.worldshili);
			System.out.println(this.worldshili);
			System.out.println(worldshili);
			w11.worldshiliMethod1();
			this.worldshiliMethod1();
			worldshiliMethod1();
			
			System.out.println(w11.worldstatic);
			System.out.println(this.worldstatic);
			System.out.println(World1.worldstatic);
			System.out.println(worldstatic);
			w11.worldstaticMethod1();
			this.worldstaticMethod1();
			World1.worldstaticMethod1();
			worldstaticMethod1();
		}
		public static void worldstaticMethod1(){}
		public static void worldstaticMethod2(){
			World1 w11=new World1();
			System.out.println(w11.worldshili);
			//System.out.println(this.worldshili);
//			System.out.println(worldshili);
			w11.worldshiliMethod1();
			//this.worldshiliMethod1();
			//worldshiliMethod1();
			
			System.out.println(w11.worldstatic);
			//System.out.println(this.worldstatic);
			System.out.println(World1.worldstatic);
			System.out.println(worldstatic);
			w11.worldstaticMethod1();
			//this.worldstaticMethod1();
			World1.worldstaticMethod1();
			worldstaticMethod1();
		}
		
	}

 静态内部类中访问外部类的元素
 构造方法---new 
 实例元素---只能对象,不能省略
 静态元素---对象/类名,可以省略

public class Test3 {
	public  String helloshili="外部类中的实例变量";
	public static String hellostatic="外部类中的静态变量";
	public Test3(){}
	public  void helloshiliMethod1(){}
	public static void hellostaticMethod1(){}
	
	public static class World2{
		public World2(){
			Test3  h2=new Test3();
			System.out.println(h2.helloshili);
			//System.out.println(Hello2.this.helloshili);
			//System.out.println(helloshili);
			h2.helloshiliMethod1();
			//Hello2.this.helloshiliMethod1();
			//helloshiliMethod1();
			System.out.println(h2.hellostatic);
			//System.out.println(Hello2.this.hellostatic);
			System.out.println(Test3.hellostatic);
			System.out.println(hellostatic);
			h2.hellostaticMethod1();
			//Hello2.this.hellostaticMethod1();
			Test3.hellostaticMethod1();
			hellostaticMethod1();
			
		}
		public  void worldshiliMethod1(){
			Test3  h2=new Test3();
			System.out.println(h2.helloshili);
			//System.out.println(Hello2.this.helloshili);
			//System.out.println(helloshili);
			h2.helloshiliMethod1();
			//Hello2.this.helloshiliMethod1();
			//helloshiliMethod1();
			System.out.println(h2.hellostatic);
			//System.out.println(Hello2.this.hellostatic);
			System.out.println(Test3.hellostatic);
			System.out.println(hellostatic);
			h2.hellostaticMethod1();
			//Hello2.this.hellostaticMethod1();
			Test3.hellostaticMethod1();
			hellostaticMethod1();
		}
		public static void worldstaticMethod1(){
			Test3  h2=new Test3();
			System.out.println(h2.helloshili);
			//System.out.println(Hello2.this.helloshili);
			//System.out.println(helloshili);
			h2.helloshiliMethod1();
			//Hello2.this.helloshiliMethod1();
			//helloshiliMethod1();
			System.out.println(h2.hellostatic);
			//System.out.println(Hello2.this.hellostatic);
			System.out.println(Test3.hellostatic);
			System.out.println(hellostatic);
			h2.hellostaticMethod1();
			//Hello2.this.hellostaticMethod1();
			Test3.hellostaticMethod1();
			hellostaticMethod1();
		}
		
	}

外部类中访问静态内部类中的元素
 构造方法--new
 实例元素--对象,不可以省略
 静态元素--对象/类名,不可以省略

public class Test4 {
	public Test4(){
		World3 w33=new World3();
		System.out.println(w33.worldshili);
		w33.worldshiliMethod1();
		System.out.println(w33.worldstatic);
		System.out.println(World3.worldstatic);
		w33.worldstaticMethod1();
		World3.worldstaticMethod1();
	}
	public  void helloshiliMethod1(){
		World3 w33=new World3();
		System.out.println(w33.worldshili);
		w33.worldshiliMethod1();
		System.out.println(w33.worldstatic);
		System.out.println(World3.worldstatic);
		w33.worldstaticMethod1();
		World3.worldstaticMethod1();
	}
	public static void hellostaticMethod1(){
		World3 w33=new World3();
		System.out.println(w33.worldshili);
		w33.worldshiliMethod1();
		System.out.println(w33.worldstatic);
		System.out.println(World3.worldstatic);
		w33.worldstaticMethod1();
		World3.worldstaticMethod1();
	}
	
	public static class World3{
		public  String worldshili="静态内部类中的实例变量";
		public static String worldstatic="静态内部类中的静态变量";
		public World3(){}
		public  void worldshiliMethod1(){}
		public static void worldstaticMethod1(){}
	}

}

他类访问静态内部类中的元素
    1.没有导入静态内部类程序包的时候
        构造方法--外部类类名.静态内部类类名 对象=new  外部类类名.静态内部类类名();
        实例元素--只能对象
        静态元素--对象/外部类类名.静态内部类类名
    2.有导入静态内部类程序包的时候
        //import com.wangxing.test1.Hello.World;
        构造方法--静态内部类类名 对象=new  外部类类名.静态内部类类名();
        实例元素--只能对象
        静态元素--对象/外部类类名.静态内部类类名

package com.wangxing.test1;
import com.wangxing.test1.Hello.World;
public class Other {
  public Other(){
	  Hello.World w=new Hello.World();
	  System.out.println(w.worldshili);
	  w.worldshiliMethod();
	  System.out.println(w.worldstatic);
	  System.out.println(Hello.World.worldstatic);
	  w.worldstaticMethod();
	  Hello.World.worldstaticMethod();
  }
  
  public void   otherShili(){
	  //import com.wangxing.test1.Hello.World;
	  Hello.World w=new Hello.World();
	  //World w=new Hello.World();
	  System.out.println(w.worldshili);
	  w.worldshiliMethod();
	  System.out.println(w.worldstatic);
	  System.out.println(Hello.World.worldstatic);
	  w.worldstaticMethod();
	  Hello.World.worldstaticMethod();
  }
  
  public static void   otherStatic(){
	  //import com.wangxing.test1.Hello.World;
	  //Hello.World w=new Hello.World();
	  World w=new Hello.World();
	  System.out.println(w.worldshili);
	  w.worldshiliMethod();
	  System.out.println(w.worldstatic);
	  System.out.println(Hello.World.worldstatic);
	  w.worldstaticMethod();
	  Hello.World.worldstaticMethod();
  }
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值