- class Test
- {
- private int num = 500;
- public void setNum(int num)
- {
- this.num = num;
- }
- public int getNum()
- {
- return this.num;
- }
-
- class TestB
- {
- public void setNum()//内部类方法,调用外部类方法用来修改外部类属性
- {
- Test.this.setNum(100);
- }
- }
-
- public static void main(String[] args)
- {
- Test a = new Test();
- Test.TestB b = a.new TestB();
- System.out.println("未被内部类修改前num的值为"+a.getNum());
-
- b.setNum();//这里的setNum()调用的是内部类中的无参数方法
-
- System.out.println("被内部类修改前num的值为"+a.getNum());
- }
- }
复制代码 |