La classe annidata Embryo
è implicitamente static
in un interface
.
Come tale, non ha accesso al metodo virtualmente invocabile bear
, che si riferisce alle istanze dell'interfaccia Mother
.
Pertanto:
- O si dichiara
Mother
come interface
, quindi i vostri Embryo
's ecluse
metodo non può praticamente invocare bear
perché è staticamente-ambito
- Oppure, si tengono
Mother
come un abstract class
, ma richiedono un istanza di Mother
(istanza di classe anonima o secondaria) per ottenere un'istanza di Embryo
(ma l'Embryo
ha l'ambito dell'istanza se non diversamente specificato e può richiamare bear
virtualmente)
autonomo esempio
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
}
}
Non completamente estranei, ma anche in realtà non rispondere alla tua domanda: http://stackoverflow.com/questions/2400828/inner-class-within- interfaccia – ctst
Potresti fornire alcune informazioni su come pensi di usare questa interfaccia/classe? – proskor