class Demo1_NoNameInnerClass {
public static void main(String[] args) {
Outer o = new Outer();
o.method();
}
}
interface Inter {
public void print();
}
class Outer {
class Inner implements Inter {
public void print() {
System.out.println("print");
}
}
public void method(){
new Inter() {
public void print() {
System.out.println("print");
}
}.print();
}
}
这是老师讲课的代码,其实我认为不用这么麻烦,请看改进版。
public class Demo1_NoNameInnerClass {
public static void main(String[] args) {
Outer.Inner oi = new Outer().new Inner();
oi.print();
}
}
interface Inter {
public void print();
}
class Outer {
class Inner implements Inter {
public void print() {
System.out.println("print");
}
}
}
一样的效果,干嘛要那么麻烦呢?
|
|