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("重写接口的抽象方法"); } }; }} 特点
- 匿名内部类没有名字,通常用于匿名实现接口或继承式结构。
- 匿名内部类可以避免创建额外的独立子类,简化了代码结构。
- 匿名内部类的优点是简洁,但缺点是不易理解和维护。
五、访问内部类的注意事项
发表评论
最新留言
很好
[***.229.124.182]2026年06月03日 07时41分13秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
PHP pcntl_fork不能在web服务器中使用的变通方法
2023-02-27
php private ,public protected三者的区别
2023-02-27
php PSR规范
2023-02-27
php redis(2)
2023-02-27
PHP Redis分布式锁
2023-02-27
PHP SOAP模块的使用方法:NON-WSDL模式
2023-02-27
PHP SPL标准库-迭代器
2023-02-27
php zookeeper实现分布式锁
2023-02-27
PHP 使用 $_SERVER['PHP_SELF'] 获取当前页面地址及其安全性问题
2023-02-27
php 反射
2023-02-27
PHP 实现N阶矩阵相乘
2023-02-28
php 延迟静态绑定static关键字
2023-02-28
Redis入门
2023-02-28
PHP 截取字符串乱码的解决方案
2023-02-28
php 接口类与抽象类的实际作用
2023-02-28
PHP 插入排序 -- 折半查找
2023-02-28
PHP 支持8种基本的数据类型
2023-02-28
php 放大镜,放大镜放大图片效果
2023-02-28
PHP 数据库连接池实现
2023-02-28
php 数组 区别,PHP中数组的区别
2023-02-28