内部类是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

最新文章

  1. HTML5将图片转化成字符画
  2. 初涉Linux ----------> Ubuntu15.04的安装与美化
  3. kafka java代码实现消费者
  4. iOS-Debug调试
  5. ASP.NET MVC的请求生命周期
  6. [Java] 使用Comparator排序对象
  7. Linux防火墙(Iptables)的开启与关闭
  8. UVA 11754 Code Feat 中国剩余定理+暴力
  9. RSA加密算法正确性证明
  10. Mybatis之动态构建SQL语句
  11. unbuntu下的root 用户和 sudo 命令
  12. Javascript 获取文档元素
  13. jQuery使用(二):DOM样式操作和属性操作
  14. 【接口时序】5、QSPI Flash的原理与QSPI时序的Verilog实现
  15. LeetCode--496--下一个更大元素I(java)
  16. Linux的安装包命令/yum 与 Rpm
  17. Application failed to start because it could not find or load the QT platform plugin “windows”
  18. c# winform 自动升级
  19. C# 给某个方法设定执行超时时间
  20. wikioi 1576 最长严格上升子序列

热门文章

  1. [HDU 3507]Print Article
  2. Evensgn 捡树枝
  3. [USACO09FEB]庙会班车Fair Shuttle
  4. HDU 5506(GT and set)
  5. bzoj 1899: [Zjoi2004]Lunch 午餐
  6. bzoj 2734: [HNOI2012]集合选数
  7. hdu5635 BestCoder Round #74 (div.2)
  8. xml 制作 RSS 订阅源
  9. sqlserver 判断字段是否为空字符串或者null
  10. Mysql bug: The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone.