为什么我不能在java中达到内部类的方法?

时间:2012-04-03 06:51:47

标签: java android eclipse inner-classes

我的结构类似于:

 class OuterClass{
        AnimatorListener sth;
        public OuterClass(){
             sth = new InnerClass();
        }
        public class InnerClass implements AnimatorListener{
             public InnerClass(){}
             public void doSomething(){}
             //assuming animator listener methods implemented
        }
        public void tryingToDoSomething(){
             sth.doSomething();   //I cannot use this, simply it is not seen by eclipse.
        }
    }

为什么我doSomething()时无法调用sth对象的tryingToDoSomething()方法?

2 个答案:

答案 0 :(得分:1)

更改构造函数语法。这是错误的

public OuterClass{
         sth = new InnerClass();
    }

将此更改为

public OuterClass(){
         sth = new InnerClass();
    }

答案 1 :(得分:1)

您的对象sth属于班级AnimatorListener,但它应属于InnerClass。希望这有帮助,快乐编码。