可以直接返回匿名内部类的对象并进行访问,可见我的例子- package exam;
- abstract class Inter {
- private int i;
- public int getNum()
- {
- return i;
- }
- abstract void setNum(int i);
- }
- class Outer
- {
- public Inter getInter()
- {
- return new Inter()
- {
- private int i;
- public int getNum()
- {
- return i;
- }
- public void setNum(int i)
- {
- this.i = i;
- }
- };
- }
- }
-
- public class Demo1 {
- public static void main(String[] args) {
- Inter inter = new Outer().getInter();
- inter.setNum(5);
- System.out.println("inter:i的值为:"+inter.getNum());
- }
- }
复制代码
答案为5 |
|