- class Demo2_NoNameInnerClass {
- public static void main(String[] args) {
- Outer out = new Outer();
- out.method();
- }
- }
- interface Show {
- void show1();
- void show2();
- }
- class Outer {
- public void method() {
- /*class Inner implements Show {
- public void show1() {
- System.out.println("SHOW1");
- }
-
- public void show2() {
- System.out.println("Show2");
- }
- }
- new Inner().show1();
- new Inner().show2();
- */
- Show s = new Show() {
- public void show1() {
- System.out.println("SHOW1");
- }
-
- public void show2() {
- System.out.println("Show2");
- }
- public void show3() {
- System.out.println("SHOW3");
- }
- };
- s.show1();
- s.show2();
- //父类引用指向子类对象,编译看左边,因为父类中没有show3()方法,故无法通过
- //s.show3();
- }
- }
复制代码 |
|