学习java内部类时一点小疑惑

qq_36743111 2018-02-17 05:51:40
// innerclasses/Ex6.java
// TIJ4 Chapter Innerclasses, Exercise 6, page 353
/* Create an interface with at least one method, in its own package. Create
* a class in a separate package. Add a protected inner class that
* implements the interface. In a third package, inherit from your class and
* inside a method, return an object of the protected inner class, upcasting
* to the interface during the return.
*/

// in separate package:
public interface Ex6Interface {
String say();
}

// and in a second package:
public class Ex6Base {
protected class Ex6BaseInner implements Ex6Interface {
// need public constructor to create one in Ex6Base child:
public Ex6BaseInner() { }
public String say() { return "Hi"; }
}
}


import innerclasses.ex6Interface.*;
import innerclasses.ex6Base.*;

public class Ex6 extends Ex6Base {
Ex6Interface getBaseInner() {
return this.new Ex6BaseInner();
}
public static void main(String[] args) {
Ex6 ex = new Ex6();
System.out.println(ex.getBaseInner().say());
}
}



为什么一定要写 public Ex6BaseInner() { } 这句话,不写就报错
...全文
1068 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

3,424

社区成员

发帖
与我相关
我的任务
社区描述
其他开发语言 其他开发语言
社区管理员
  • 其他开发语言社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧