このクラスを持つ
public abstract class Mother{
public class Embryo{
public void ecluse(){
bear(this);
}
}
abstract void bear(Embryo e);
}
母のインスタンスがある場合にのみ、胚のインスタンスを作成できます。
new Mother(){...}.new Embryo().ecluse();
質問:
ネストされたクラスEmbryo
は暗黙的static
にinterface
。にあります。
そのため、インターフェースのbear
インスタンスに関係する、事実上呼び出し可能なメソッドにアクセスできませんMother
。
したがって:
Mother
のようにinterface
、あなたの、Embryo
のecluse
方法は事実上、呼び出すことはできませんbear
、それは静的スコープますので、Mother
として保持しabstract class
ますが、Mother
のインスタンスを取得するために(匿名または子クラスのインスタンス)のインスタンスが必要ですEmbryo
(ただしEmbryo
、特に指定されていない限り、インスタンススコープであり、bear
仮想的に呼び出すことができます)自己完結型の例
package test;
public class Main {
public interface MotherI {
// this is static!
public class Embryo {
public void ecluse() {
// NOPE, static context, can't access instance context
// bear(this);
}
}
// implicitly public abstract
void bear(Embryo e);
}
public abstract class MotherA {
public class Embryo {
public void ecluse() {
// ok, within instance context
bear(this);
}
}
public abstract void bear(Embryo e);
}
// instance initializer of Main
{
// Idiom for initializing static nested class
MotherI.Embryo e = new MotherI.Embryo();
/*
* Idiom for initializing instance nested class
* Note I also need a new instance of `Main` here,
* since I'm in a static context.
* Also note anonymous Mother here.
*/
MotherA.Embryo ee = new MotherA() {public void bear(Embryo e) {/*TODO*/}}
.new Embryo();
}
public static void main(String[] args) throws Exception {
// nothing to do here
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加