Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

java - Puzzle on Inner Classes and Class Hiding

I have a sample code to solve which is based on inner classes:

package inner;

class A {
    void m() {
        System.out.println("Outer");
    }
}

public class TestInner {
    public static void main(String[] args) {
        new TestInner().go();       
    }

    private void go() {
        new A().m();
        class A{
            void m(){
                System.out.println("Inner");
            }
        }
        new A().m();
    }
    class A{
            void m(){
            System.out.println("Middle");
        }
    }
}

The output given by above sample code is:

Middle 
Inner

And my question is, given that I dont want to use the package name to create an object, how can I print the output as:

Outer
Inner
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since using a package is so obviously the answer, I assume you are looking for something obtuse.

You can add an outer class and call that.

class B extends A { }

// in TestInner.go()
    new B().m();
    class A{
        void m(){
            System.out.println("Inner");
        }
    }
    new A().m();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...