有这个课
public abstract class Mother{
public class Embryo{
public void ecluse(){
bear(this);
}
}
abstract void bear(Embryo e);
}
Run Code Online (Sandbox Code Playgroud)
只有我有一个母亲的实例,我才能创建一个胚胎实例:
new Mother(){...}.new Embryo().ecluse();
Run Code Online (Sandbox Code Playgroud)
题:
嵌套类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
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
118 次 |
| 最近记录: |