Java基础知识、十二(内部类)
发布日期:2021-04-30 21:04:03 浏览次数:105 分类:精选文章

本文共 5220 字,大约阅读时间需要 17 分钟。

Java内部类详解

一、内部类的定义

内部类是指定义在另一个类(称为外部类)内部的类。与外部类相比,内部类具有一些独特的特性和访问规则。外部类包含内部类的类。

内部类的编译结果

内部类在编译后会生成一个新的字节码文件,其文件名格式为:外部类类名$内部类类型.class。


二、内部类的表现形式

内部类可以分为以下几种形式,各自具有不同的特点和编写方式。

1. 成员内部类 - 类中方法外

成员内部类是指定义在外部类的方法或构造器中。

示例代码

public class Hello {    public static String HELLO_STATIC_NAME = "外部静态变量";    public Hello() {        // 外部类的构造方法        System.out.println("外部类的构造方法");        World wneibu = new World("world");        System.out.println(wneibu.world_name);        wneibu.worldShiliMethod();    }    public void helloShiliMethod() {        // 外部类的实例方法        System.out.println("外部类的实例方法");        World wneibu = new World("world");        System.out.println(wneibu.world_name);        wneibu.worldShiliMethod();    }    public static void helloStaticMethod() {        // 外部类的类方法        System.out.println("外部类的类方法(静态方法)");        Hello ha = new Hello();        World wneibu = ha.new World("world");        System.out.println(wneibu.world_name);        wneibu.worldShiliMethod();        System.out.println(World.wostatic);    }}public class World {    public static final String wostatic = "静态常量";    public String world_name = "内部类的实例变量";    public World(String testname) {        System.out.println("内部类带参数的构造方法,参数是==" + testname);    }    public void worldShiliMethod() {        System.out.println("内部类的实例方法");    }    public World() {        System.out.println("内部类的构造方法");        World w = new World("world");        System.out.println(w);        System.out.println(this.world_name);        System.out.println(world_name);        w.worldShiliMethod();        this.worldShiliMethod();        worldShiliMethod();    }}

特点

  • 成员内部类可以使用任意访问限制修饰符。
  • 成员内部类可以包含实例变量、静态常量、实例方法和构造方法,但不能包含静态方法。
  • 成员内部类的构造方法可以访问外部类的构造方法、实例方法和静态方法,但需要通过外部类对象来限定分配。

2. 方法内部类 - 类中方法中

方法内部类是指定义在外部类的方法中,通常与方法的局部变量相关联。

示例代码

public class Hello {    public static String HELLO_STATIC_NAME = "外部类变量";    public Hello() {        System.out.println("外部类的构造方法");    }    public void helloShiliMethod() {        System.out.println("外部类的实例方法");        class WorldInner {            public String worldinner_name = "方法内部类的实例变量";            public final static String worldstatic_name = "方法内部类的静态常量";            public WorldInner(String testname) {                System.out.println("方法内部类的构造方法,参数是==" + testname);            }            public void woldinnerShiliMethod1() {                System.out.println("方法内部类的实例方法");            }        }        // 方法内部类的构造方法        WorldInner inner = new WorldInner("test");        System.out.println(inner.worldinner_name);        System.out.println(this.worldinner_name);        System.out.println(worldinner_name);        inner.woldinnerShiliMethod1();        this.woldinnerShiliMethod1();        woldinnerShiliMethod1();        // 方法内部类的实例方法        public void woldinnerShiliMethod2() {            System.out.println("方法内部类的实例方法");            WorldInner inner = new WorldInner("test");            System.out.println(inner.worldinner_name);            System.out.println(this.worldinner_name);            System.out.println(worldinner_name);            inner.woldinnerShiliMethod1();            this.woldinnerShiliMethod1();            woldinnerShiliMethod1();        }    }    public static void helloStaticMethod() {        System.out.println("外部类的类方法");    }}

特点

  • 方法内部类不能使用任何访问限制修饰符。
  • 方法内部类可以包含实例变量、实例方法和构造方法,但不能包含静态方法。
  • 方法内部类的构造方法可以访问外部类的构造方法、实例方法和静态方法。
  • 方法内部类可以访问本方法的局部变量。

三、静态嵌套类

静态嵌套类是指在外部类中定义为静态成员的内部类。静态嵌套类和外部类之间没有this关系,可以独立存在。

示例代码

public class Hello {    public static String HELLO_STATIC_NAME = "外部类类变量";    public Hello() {        System.out.println("外部类的构造方法");    }    public void helloShiliMethod() {        System.out.println("外部类的实例方法");    }    public static void helloStaticMethod() {        System.out.println("外部类的类方法");    }}public static class World {    public static final String world_static_name = "静态嵌套类类变量";    public String world_name = "静态嵌套类实例变量";    public World(String testname) {        System.out.println("静态嵌套类的构造方法,参数是==" + testname);    }    public void worldShiliMethod() {        System.out.println("静态嵌套类的实例方法");    }    public static void worldStaticShiliMethod() {        System.out.println("静态嵌套类的类方法");    }}

特点

  • 静态嵌套类可以包含实例变量、实例方法、构造方法和类变量、类方法。
  • 静态嵌套类的构造方法可以访问外部类的构造方法、实例方法和静态方法。
  • 静态嵌套类中的类方法可以访问外部类的构造方法、实例方法和静态方法。

四、匿名内部类

匿名内部类是没有名字的内部类,通常用于匿名实现接口或继承式结构。

示例代码

public class MyClass {    public void testMyClass() {        System.out.println("MyClass类的实例方法");    }}public class Test1 {    public static void main(String[] args) {        // 匿名内部类继承式        MyClass mc = new MyClass() {            @Override            public void testMyClass() {                System.out.println("重写MyClass类的testMyClass方法");            }        };        mc.testMyClass();    }}

示例代码(接口实现)

public interface MyInterface {    void testMethod();}public class Test2 {    public static void main(String[] args) {        // 匿名内部类实现接口        MyInterface inter = new MyInterface() {            @Override            public void testMethod() {                System.out.println("重写接口的抽象方法");            }        };    }}

特点

  • 匿名内部类没有名字,通常用于匿名实现接口或继承式结构。
  • 匿名内部类可以避免创建额外的独立子类,简化了代码结构。
  • 匿名内部类的优点是简洁,但缺点是不易理解和维护。

五、访问内部类的注意事项

  • 其他类中访问内部类时,需要依赖外部类对象来限定分配。
  • 导入内部类所在的包,或者使用外部类类名.内部类类名来创建对象。
  • 内部类的访问权限由其定义的访问修饰符决定。
  • 上一篇:eclipse如何以javadoc方式查看源码的注释
    下一篇:java获取资源文件的各种方法

    发表评论

    最新留言

    很好
    [***.229.124.182]2026年06月03日 07时41分13秒