一、题目要求
按照要求补齐代码,并在控制台输出"HelloWorld"。
interface Inter {
void show();
}
class Outer {
//补齐代码
}
class OuterDemo {
public static void main(String[] args) {
Outer.method().show();
}
}
二、题目详解
interface Inter {
void show(); //把隐藏修饰符加上之后相当于 public abstract void show();
}
class Outer {
//补齐代码
/*
a、通过下面那三个分析可以写出public static Inter method;
b、由于接口返回的是子类(实现类)的对象,可以用子类匿名对象;
c、new Inter() {
public void show() {
System.out.println("HelloWorld");
}
}
这整体是一个子类对象;
d、用Inter接收返回的子类对象。
*/
//-----此题答案在两线之间---------------
public static Inter method() {
return new Inter() {
public void show() {
System.out.println("HelloWorld");
}
};
}
//-----此题答案在两线之间---------------
}
class OuterDemo {
public static void main(String[] args) {
Outer.method().show();//链式编程
/*
1、Outer.method()可以看出method()应该是Outer中的一个静态方法;
2、method()还可以调方法,说明它有返回值,并且返回值是一个对象;
3、method()可以调用show()方法,说明返回值类型为Inter接口,因为只有Inter下面才是show()方法;
*/
}
}