完全可以,看下边的例子,摘自Think in Java,The 2nd Edition.先定义一个接口: interface Contents { int value(); }再定义一个类(构造函数不是默认的): public class Wrapping { private int i; public Wrapping(int x) { i = x; } public int value() { return i; } }先实现接口: public class Parcel6 { public Contents cont() { return new Contents() { private int i = 11; public int value() { return i; } }; // Semicolon required in this case } public static void main(String[] args) { Parcel6 p = new Parcel6(); Contents c = p.cont(); } }再继承类: public class Parcel7 { public Wrapping wrap(int x) { // Base constructor call: return new Wrapping(x) { public int value() { return super.value() * 47; } }; // Semicolon required } public static void main(String[] args) { Parcel7 p = new Parcel7(); Wrapping w = p.wrap(10); } } 更详细请看Think in Java,现在是The 3rd Edition.
是啊,Think in java 上面说的很清楚,肯定是对的,自己看看
一楼的解释说得很有问题, 二楼说说得很清楚了,自己看看,基本属于废话首先楼主指的无名内部类实现接口指的是类本身定义的时候实现接口还是直接定义无名内部类的时候实现接口,前者肯定是可以的,后者应该是不行的,光从语法上都不能实现 class A implements SomeInterface{...}//这里实现接口肯定是可以的 class Main{ ... public A f(){ return new A(){...}//这里定义了一个无名内部类,要实现接口,语法上都是不能实现的 } ... }一楼的“先实现接口”“再继承类”,简直不知所谓
to: believefym(暮色,miss,迷失,miss) 你的 return new A(){...}; 是实现了接口A,而不仅是只定义了一个内部类。你可以自己在A接口中定义一个方法而不在return new A(){...}; 中实现,看看什么效果。匿名内部类要实现或者继承类就是这种语法,当然匿名内部类是不能用implements,extends关键字来实现或继承其它类的。
匿名内部类如何创建,new ***** 它天生就必须以父类或父接口的名字为名字,随便写一个代码:class A { I getI(final String str){ return new I(){ public void dosth(){ System.out.print(str);}}; } public static void main(String args[]) { new A().getI("hi").dosth(); //System.out.print((new A()).getI("hi").dosth()); } }interface I{ void dosth();}
new I(){…… 有趣吗?建议楼主 把I写成抽象类、普通类玩一玩。believefym(暮色,miss,迷失,miss) 的话是中肯的,仅仅在接口上有点想当然。
interface Contents {
int value();
}再定义一个类(构造函数不是默认的):
public class Wrapping {
private int i;
public Wrapping(int x) { i = x; }
public int value() { return i; }
}先实现接口:
public class Parcel6 {
public Contents cont() {
return new Contents() {
private int i = 11;
public int value() { return i; }
}; // Semicolon required in this case
}
public static void main(String[] args) {
Parcel6 p = new Parcel6();
Contents c = p.cont();
}
}再继承类:
public class Parcel7 {
public Wrapping wrap(int x) {
// Base constructor call:
return new Wrapping(x) {
public int value() {
return super.value() * 47;
}
}; // Semicolon required
}
public static void main(String[] args) {
Parcel7 p = new Parcel7();
Wrapping w = p.wrap(10);
}
} 更详细请看Think in Java,现在是The 3rd Edition.
二楼说说得很清楚了,自己看看,基本属于废话首先楼主指的无名内部类实现接口指的是类本身定义的时候实现接口还是直接定义无名内部类的时候实现接口,前者肯定是可以的,后者应该是不行的,光从语法上都不能实现
class A implements SomeInterface{...}//这里实现接口肯定是可以的
class Main{
...
public A f(){
return new A(){...}//这里定义了一个无名内部类,要实现接口,语法上都是不能实现的
}
...
}一楼的“先实现接口”“再继承类”,简直不知所谓
它天生就必须以父类或父接口的名字为名字,随便写一个代码:class A {
I getI(final String str){
return new I(){
public void dosth(){
System.out.print(str);}}; }
public static void main(String args[]) {
new A().getI("hi").dosth();
//System.out.print((new A()).getI("hi").dosth());
}
}interface I{ void dosth();}
有趣吗?建议楼主 把I写成抽象类、普通类玩一玩。believefym(暮色,miss,迷失,miss) 的话是中肯的,仅仅在接口上有点想当然。