interface Inter {
void show();
}
class Outer {
//请在此处补齐代码
}
class OuterDemo {
public static void main(String[] args) {
Outer.method().show();
}
}
需求:要求在控制台输出”HelloWorld”
分析: Outer.method().show();
Outer.method():
1)使用类名访问,所以访问的"对象"是静态类或者静态方法(且为public,因为外界可直接访问)
2) 由于不是new Outer.method() ,所以访问的不是静态成员内部类,那么就只能是共有的静态方法。(其实看method全小写命名说明它就是个方法)
3)接口中有show方法的申明,那么一定涉及重写——》实现类
4)解决:普通的、有名字的内部类、【匿名内部类】
解答:
package com.first.demos.day09.homeworks;
/*
* 2、 interface Inter {
void show();
}
class Outer {
//请在此处补齐代码
}
class OuterDemo {
public static void main(String[] args) {
Outer.method().show();
}
}
要求在控制台输出”HelloWorld”
* */
public class homework02 {
public static void main(String[] args) {
Outer.method().show();
}
}
interface Inter{
void show();
}
class Outer{
//注意返回值类型为Inter。因为内部类外部是不可见的
public static Inter method() {
//普通内部类
class Son implements Inter{
@Override
public void show() {
System.out.println("HelloWorld");
}
}
//这里要实例化,因为返回的是一个对象
return new Son();
}
// public static Inter method() {
//匿名内部类
// return new Inter() {
// @Override
// public void show() {
// System.out.println("HelloWorld");
// }
// };
// }
//这个要加new
// static class method implements Inter{
//
// @Override
// public void show() {
// System.out.println("HelloWorld!!!");
// }
//
// }
}