Javaに基本クラスがあります。そのクラスでプライベートクラスを作成し、基本クラスのそのプライベートクラスのオブジェクトにアクセスしたいと思います。どうやってやるの?
前もって感謝します!
Javaに基本クラスがあります。そのクラスでプライベートクラスを作成し、基本クラスのそのプライベートクラスのオブジェクトにアクセスしたいと思います。どうやってやるの?
前もって感謝します!
You can access an object of an inner class by creating it and remembering its reference. Just like an instance of any other class.
public enum Outer {;
    private static class Nested {
        private Nested() { }
    }
    public static Object getNested() {
        return new Nested();
    }
}
public class Main {
    public static void main(String... args)  {
        System.out.println("I have an "+ Outer.newNested());
    }
}
prints
I have an Outer$Nested@3f0ef90c
A good example is from Arrays. This creates an instance of a private nested class which implements a public interface which makes it useful.
public static <T> List<T> asList(T... a) {
return new ArrayList<T>(a);
}
/**
 * @serial include
 */
private static class ArrayList<E> extends AbstractList<E>
implements RandomAccess, java.io.Serializable
{
    これを意味しますか:
class Test {
    private Inner inner = new Inner();  
    private class Inner {
        public void foo() {}
    }
    // later somewhere
    public void bar() {
        inner.foo();
    }      
}
    PrivateClass c = new PrivateClass();
c.getSomeObject(); //??
上記のコードを基本クラスで使用できます。プライベートクラスが基本クラスの内部クラスである場合。
class Test {
    private class Inner {
        public void foo() {
            System.out.println("vsahdashdashd");
        }
    }
    // later somewhere
    public void bar() {
        new Inner().foo();
    }      
}
class javaapplication9 extends Test
{
      public static void main(String[] args) {
        Test inner = new Test();  
        inner.bar();
      }
you can access private member from this type.