Java实现内部类
2024-09-11 10:06:05
内部类是java中非常方便的一种机制,内部类所在的类称为宿主类,即内部类只能被它的宿主类使用,用这个特性,可以很好的控制类的可见性。
接下来看一个例子:
package for_apro; import java.util.*; class outObject{ private int id ; //在宿主类实现方法实例化内部类并访问内部类的方法 void user_set_int(int id) { intObject init = new intObject(); //实例化内部类 init.set(id); } void user_print_int() { intObject init = new intObject(); init.print(); } //定义一个内部类 class intObject{ void set(int id) { outObject.this.id = id ; //内部类设置宿主类的成员 } void print() { System.out.println(outObject.this.id); //内部类打印宿主类成员 } } } public class first_for { public static void main(String [] args){ outObject out = new outObject(); out.user_set_int(10086); out.user_print_int(); } }
执行结果:10086
最新文章
- HTML5将图片转化成字符画
- 初涉Linux ---------->; Ubuntu15.04的安装与美化
- kafka java代码实现消费者
- iOS-Debug调试
- ASP.NET MVC的请求生命周期
- [Java] 使用Comparator排序对象
- Linux防火墙(Iptables)的开启与关闭
- UVA 11754 Code Feat 中国剩余定理+暴力
- RSA加密算法正确性证明
- Mybatis之动态构建SQL语句
- unbuntu下的root 用户和 sudo 命令
- Javascript 获取文档元素
- jQuery使用(二):DOM样式操作和属性操作
- 【接口时序】5、QSPI Flash的原理与QSPI时序的Verilog实现
- LeetCode--496--下一个更大元素I(java)
- Linux的安装包命令/yum 与 Rpm
- Application failed to start because it could not find or load the QT platform plugin “windows”
- c# winform 自动升级
- C# 给某个方法设定执行超时时间
- wikioi 1576 最长严格上升子序列
热门文章
- [HDU 3507]Print Article
- Evensgn 捡树枝
- [USACO09FEB]庙会班车Fair Shuttle
- HDU 5506(GT and set)
- bzoj 1899: [Zjoi2004]Lunch 午餐
- bzoj 2734: [HNOI2012]集合选数
- hdu5635 BestCoder Round #74 (div.2)
- xml 制作 RSS 订阅源
- sqlserver 判断字段是否为空字符串或者null
- Mysql bug: The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone.