有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java可以直接用InnerClassNameOuter而不是ClassName来指定内部类。内部类名

在Java的内部思维类中

If you want to make an object of the inner class anywhere except from within a non-static method of the outer class, you must specify the type of that object as OuterClassName.InnerClassName, as seen in main().

但我发现直接使用InnerClassName在main中仍然有效

   public class Parcel2 {
        class Contents {
            private int i = 42;
            public int value() { return i; }
        }
        class Destination {
            private String label;
            Destination(String whereTo){
                label = whereTo;
            }
            String readLabel(){ return label; }
        }

        public Destination to(String s){
            return new Destination(s);
        }

        public static void main(String[] args){
            Parcel2 q = new Parcel2();  

            /* Destionation d = q.to("Borneo");      still works.*/ 
            Parcel2.Destination d = q.to("Borneo"); 
        }
    }

共 (0) 个答案