[英]How to extends Abstract Inner Class in java
I confused if 我很困惑
Abstract Class A{method();method2();}
And Other Class B
Which Have Inner Class C
和其他具有内部Class C
Class B
Class C
Class B{Abstract Class C{method(){//body}}}
And now Question is how to extends Class C b/C Abstract Class must be extends else this is Unused class. 现在的问题是如何扩展C类b / C抽象类必须扩展,否则这是未使用的类。
First, let's make it simpler - this has nothing to do with Android directly, and you don't need your A
class at all. 首先,让我们变得更简单-这与Android直接无关,并且您根本不需要A
类。 Here's what you want: 这就是您想要的:
class Outer {
abstract class Inner {
}
}
class Child extends Outer.Inner {
}
That doesn't compile, because when you create an instance of Child
you need to provide an instance of Outer
to the Inner
constructor: 那不会编译,因为在创建Child
的实例时,您需要向Inner
构造函数提供Outer
的实例:
Test.java:6: error: an enclosing instance that contains Outer.Inner is required
class Child extends Outer.Inner {
^
1 error
There are two options that can fix this: 有两个选项可以解决此问题:
If you don't need to refer to an implicit instance of Outer
from Inner
, you could make Inner
a static nested class instead: 如果不需要从Inner
引用Outer
的隐式实例,则可以使Inner
成为静态嵌套类:
static abstract class Inner { }
You could change Child
to accept a reference to an instance of Outer
, and use that to call the Inner
constructor, which uses slightly surprising syntax, but works: 您可以将Child
更改为接受对Outer
实例的引用,然后使用该实例调用Inner
构造函数,该构造函数使用有点令人惊讶的语法,但可以:
Child(Outer outer) { // Calls Inner constructor, providing // outer as the containing instance outer.super(); }
Note that these are alternatives - you should pick which one you want based on whether or not the inner class really needs to be an inner class. 请注意,这些是替代方法-您应该根据内部类是否确实需要成为内部类来选择所需的那个。
You simply extend it 您只需扩展它
class B{abstract class C{abstract void method();}}
class D extends B{
class E extends C{
void method(){
System.out.println("Hello World");
}
}
}
Or slightly more complicated without extending outer class 或者稍微复杂一点而不扩展外部类
class B{abstract class C{abstract void method();}}
public class F extends B.C{
F(B b){
b.super();
}
void method(){
System.out.println("Hello World");
}
public static void main(String[] args){
B b = new B();
F f = new F(b);
f.method();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.