o(︶︿︶)o 唉怎么在最后两天懵了。。。。好蛋疼啊啊啊啊啊啊啊啊
贴俩代码
- class Demo2 {
- 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() {
- //Inner i = new Inner();
- //i.print();
- //new Inner().print();
- new Inter() { //实现Inter接口
- public void print() { //重写抽象方法
- System.out.println("print");
- }
- }.print();
- }
-
- }
复制代码
- class Demo3 {
- public static void main(String[] args) {
- Outer o = new Outer();
- o.method();
- }
- }
- interface Inter {
- public void show1();
- public void show2();
- }
- //匿名内部类只针对重写一个方法时候使用
- class Outer {
- public void method() {
- /*new Inter() {
- public void show1() {
- System.out.println("show1");
- }
- public void show2() {
- System.out.println("show2");
- }
- }.show1();
- new Inter() {
- public void show1() {
- System.out.println("show1");
- }
- public void show2() {
- System.out.println("show2");
- }
- }.show2();*/
- Inter i = new Inter() {
- public void show1() {
- System.out.println("show1");
- }
- public void show2() {
- System.out.println("show2");
- }
- /*public void show3() {
- System.out.println("show2");
- }*/
- };
- i.show1();
- i.show2();
- //i.show3(); //匿名内部类不能向下转型,没有子类类名
- }
- }
复制代码 |
|