将一个java类定义到另一个类的类体中,那么定义在类体中的类就是内部类
我们把包含内部类的java类叫外部类
- public class Hello{
- public class World{
-
- }
- }
Hello---外部类
World---内部类
内部类在编译之后会得到独立的内部类的字节码文件【外部类的类名$内部类的类名.class】
1.成员内部类
2.方法内部类
3.匿名内部类
4.静态内部类【静态嵌套类】
public class Hello{
//成员内部类
public class World{
}
}
1.成员内部类中的元素:实例变量,构造方法,实例方法;不能出现静态元素
- package com.wangxing.chengyuan.test1;
- public class Hello {
- //成员内部类
- public class World{
- public String worldshili="成员内部类中的实例变量";
- //不能出现静态变量
- //public static String worldstatic="成员内部类中的静态变量";
- public World(){
- System.out.println("成员内部类中的构造方法");
- }
- public void worldShiLiMethod(){
- System.out.println("成员内部类中的实例方法");
- }
- //不能出现静态方法
- //public static void worldStaticMethod(){
- // System.out.println("成员内部类中的静态方法");
- //}
- }
- }
2.成员内部类中可以访问成员内部类中的实例变量/实例方法,构造方法
2.1.对实例变量/实例方法的访问,对象.实例变量/实例方法,this.实例变量/实例方法,对象和this可以省略
2.2.对构造方法的访问,通过new访问。
- package com.wangxing.chengyuan.test1;
- public class Hello2 {
- //成员内部类
- public class World2{
- public String worldshili="成员内部类中的实例变量";
-
- public World2(){
- System.out.println("成员内部类中的构造方法");
- }
-
- public World2(String name){
- World2 w2=new World2();
- System.out.println(w2.worldshili);
- System.out.println(this.worldshili);
- System.out.println(worldshili);
- w2.worldShiLiMethod1();
- this.worldShiLiMethod1();
- worldShiLiMethod1();
- }
- public void worldShiLiMethod1(){
- System.out.println("成员内部类中的实例方法");
- }
-
- public void worldShiLiMethod2(){
- System.out.println("成员内部类中的实例方法");
- World2 w2=new World2();
- System.out.println(w2.worldshili);
- System.out.println(this.worldshili);
- System.out.println(worldshili);
- w2.worldShiLiMethod1();
- this.worldShiLiMethod1();
- worldShiLiMethod1();
- }
- }
- }
3.成员内部类中可以访问外部类的实例变量/实例方法,静态变量/静态方法,构造方法
3.1.对实例变量/实例方法的访问,对象.实例变量/实例方法,外部类名.this.实例变量/实例方法,对象/外部类名.this可以省略。
3.2.对静态变量/静态方法的访问,对象.静态变量/静态方法,外部类名.this.静态变量/静态方法,外部类类名.静态变量/静态方法,对象/外部类名.this/外部类类名可以省略。
3.3.对构造方法访问,通过new访问
- package com.wangxing.chengyuan.test1;
- public class Hello3 {
- public String helloshili="外部类中的实例变量";
- public static String hellostatic="外部类中的静态变量";
- public Hello3(){
- System.out.println("外部类的构造方法");
- }
- public void helloShiLi(){
- System.out.println("外部类的实例方法");
- }
- public static void helloStatic(){
- System.out.println("外部类的静态方法");
- }
- //成员内部类
- public class World3{
- public World3(){
- Hello3 h3=new Hello3();
- System.out.println(h3.helloshili);
- System.out.println(Hello3.this.helloshili);
- System.out.println(helloshili);
- System.out.println(h3.hellostatic);
- System.out.println(Hello3.this.hellostatic);
- System.out.println(Hello3.hellostatic);
- System.out.println(hellostatic);
- h3.helloShiLi();
- Hello3.this.helloShiLi();
- helloShiLi();
- h3.helloStatic();
- Hello3.this.helloStatic();
- Hello3.helloStatic();
- helloStatic();
- }
- public void worldShiLiMethod1(){
- Hello3 h3=new Hello3();
- System.out.println(h3.helloshili);
- System.out.println(Hello3.this.helloshili);
- System.out.println(helloshili);
- System.out.println(h3.hellostatic);
- System.out.println(Hello3.this.hellostatic);
- System.out.println(Hello3.hellostatic);
- System.out.println(hellostatic);
- h3.helloShiLi();
- Hello3.this.helloShiLi();
- helloShiLi();
- h3.helloStatic();
- Hello3.this.helloStatic();
- Hello3.helloStatic();
- helloStatic();
- }
- }
- }
4.外部类中可以访问成员内部类中的元素
4.1.外部类中的构造方法/实例方法中可以访问成员内部类中的实例变量/实例方法,构造方法,只能对象访问,不能省略。
4.2.在外部类中的静态方法中不能访问成员内部类中的元素。
- package com.wangxing.chengyuan.test1;
- public class Hello4 {
- public String helloshili="外部类中的实例变量";
- public static String hellostatic="外部类中的静态变量";
- public Hello4(){
- 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("成员内部类中的实例方法");
- }
- }
- }
5.其他类中访问成员内部中的元素
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();
- }
- }