文章目录
内部类:将一个类的定义放在另一个类的定义内部。
优点:把一些逻辑相关的类组织在一起(优雅清晰),控制内部类的可视性。
10.1 创建内部类
把类的定义置于外围类的里面
public class Parcel1 {
class Destination {
private String label;
Destination(String whereTo) {
label = whereTo;
}
String readLabel() {
return label;
}
}
static class Destination1 {
private String label;
Destination1(String whereTo) {
label = whereTo;
}
String readLabel() {
return label;
}
}
public Destination to(String s) {
return new Destination(s);
}
public static void main(String[] args) {
Parcel1.Destination d1 = new Parcel1().to("d1"); // 外部对象调方法获取内部对象返回值
Parcel1.Destination d2 = new Parcel1().new Destination("d2");// 外部对象调内部类构造方法
Parcel1.Destination1 d3 = new Parcel1.Destination1("d3");// 内部类为static,以外部类调用
System.out.println(d1.readLabel());
System.out.println(d2.readLabel());
System.out.println(d3.readLabel());
}
}
10.2 链接到外部类
当生成一个内部类的对象时,此对象与制造它的外围对象(enclosing object)之间就有了一种联系,所以它能访问其外围对象的所有成员,而不需要任何特殊条件。此外,内部类还拥有其外围类的所有元素的访问权。
public class Parcel1 {
private String label;
class Destination {
Destination(String whereTo) {
label = whereTo;
}
String readLabel() {
return label;
}
}
public Destination to(String s) {
return new Destination(s);
}
public static void main(String[] args) {
Parcel1.Destination d1 = new Parcel1().to("d1");
System.out.println(d1.readLabel());
}
}
10.3 使用.this与.new
如果需要生成对外部类对象的引用,可以使用外部类的名字后面紧跟圆点和this。
https://blog.csdn.net/li_xunhuan/article/details/98657521
public class Parcel3 {
public Parcel3 f() {
return this; // 返回当前类对象
}
public class Inner {
public Parcel3 outer() {
return Parcel3.this; // 返回外部类对象
}
public Inner f() {
return this; // 返回当前类对象
}
}
public Inner inner() {
return new Inner();
}
public static void main(String[] args) {
Parcel3 a = new Parcel3();
Parcel3 b = a.f();
Parcel3.Inner inner1 = new Parcel3().new inner();
Parcel3.Inner inner2 = inner1.f();
Parcel3 c = inner2.outer().f();
}
}
创建某个内部类对象,使用.new语法,内部类对象会暗暗连接到创建它的外部类对象上。因此无法在未创建外部类对象之前创建内部类对象,除非是静态内部类。
public class DotNew {
public class Inner {}
public static void mian(String[] args) {
DotNew dn = new DotNew();
DotNew.Inner dni = dn.new Inner();
}
}
10.4 内部类与向上转型
内部类——某个接口的实现,可以设为private,完全不可见,能够很方便的隐藏实现细节。
public class Parcel3 {
public void f() {
System.out.println("Parcel3......");
}
interface ite {
Parcel3 outer();
}
private class Inner implements ite {
public Parcel3 outer() {
return Parcel3.this;
}
}
public Inner inner() {
return new Inner();
}
public static void mian(String[] args) {
Inner inner = new Parcel3().inner();
inner.outer().f();
}
}
10.5 在方法和作用域内的内部类
在方法的作用域内创建一个完整的类——局部内部类
PDestination是destination()方法的一部分,而不是Parcel4的一部分,所以,在destination()之外不能访问PDestination。
public class Parcel4 {
private void a() {
System.out.println("a......");
}
public Destination destination(String s) {
class PDestination implements Destination {
private String label;
private PDestination(String whereTo) {
label = whereTo;
}
public String getLabel() {
a(); // 可以调用外部类的元素
return label;
}
}
return new PDestination(s);
}
public interface Destination {
String getLabel();
}
public static void mian(String[] args) {
Parcel4.Destination p = new Parcel4().destination("Tesla");
System.out.println(d.getLabel());
}
}
10.6 匿名内部类
匿名内部类既可以拓展类,也可以拓展接口,但是不能两者兼备。如果实现接口,只能实现一个接口。
使用默认的构造器
创建的这个类是没有名字的,它实现了Contents接口,但是由于只是用一次,因此没有在外面定义出来。具体的说,创建了一个继承自Contents的匿名类的对象。通过new表达式返回的引用被自动向上转型为对Contents的引用。
interface Contents {
int value();
}
public class Parcel5 {
public Contents contents() {
return new Contents() {
private int i = 11;
public int value() {
return i;
}
};
}
public static void main(String[] args) {
Contents c = new Parcel5().contents();
System.out.println(c.value());
}
}
使用有参数的构造器
将参数传递给基类的构造器。
class Contents {
int i;
Contents(int x) {
i = x;
}
public int value() {
return i;
}
}
public class Parcel5 {
public Contents contents(int x) {
return new Contents(x) {
public int value() {
return super.value();
}
};
}
public static void main(String[] args) {
Contents c = new Parcel5().contents(9);
System.out.println(c.value());
}
}
在匿名内部类中使用一个在其外部定义的对象,那么其编译器会要求其参数引用是final的
java8开始,可以不加final修饰符,由系统默认添加。java将这个功能称为:Effectively final 功能。
interface Contents {
public int value();
}
public class Parcel5 {
public Contents contents(final String x) { // 此处为final
return new Contents() {
private String i = x; // 此处用到外部对象
public String value() {
return i;
}
};
}
public static void main(String[] args) {
Parcel5 p = new Parcel5();
Contents c = p.contents("aaaaa");
System.out.println(c.value());
}
}
再访工厂方法(略)
10.7 嵌套类
- 如果不需要内部类对象与外围对象之间有联系,那么可以将内部类声明为static,即嵌套类。
- 普通的内部类对象隐含的保存了一个引用,指向创建它的外围对象。然而当内部类是static的,意味着创建嵌套类对象不需要其外围对象,不能从嵌套类对象中访问非静态的外围类对象。
- 普通内部类不能有static字段,不能包含嵌套类,嵌套类可以
接口内部的类
- 正常情况下不能在接口内部放置任何代码,但嵌套类可以作为接口的一部分。放到接口中的任何类都是public和static的。因为类的static的,只是将嵌套类置于接口的命名空间。
- 如果想要创建某些公共代码,使得接口的不同实现所公用,那么可以使用接口的嵌套类。
public interface Parcel5 {
void f();
class contents implements Parcel5 {
public void f() {
System.out.println("aaaa");
}
public static void main(String[] args) {
new contents.f();
}
}
}
- 可以在嵌套类中使用main()作为测试类,这样在外部类中不会有额外的代码。
public class Parcel6 implements Parcel5 {
public void f() {
new Parcel5.contents().f();
System.out.println("bbbbb");
}
public static class test {
public static void main(String[] args) {
Parcel6 p = new Parcel6();
p.f();
}
}
}
从多层嵌套类中访问外部类的成员
一个内部类被嵌套多少层并不重要——它能透明地访问所有它所嵌入的外围类的所有成员
public class Parcel7 {
class A {
private void f() {
System.out.println("fffff");
}
class B {
class C {
void h() {
f();
}
}
}
}
public static class D {
public static void main(String[] args) {
Parcel7 a = new Parcel7();
Parcel7.A a1 = a.new A();
Parcel7.A.B B = a1.new B();
Parcel7.A.B.C C = b.new C();
c.h();
}
}
}
10.8 为什么需要内部类
内部类使得多重继承的解决方案变得完整。接口解决了部分问题,而内部类有效地实现了“多重继承”。也就是说,内部类允许继承多个非接口类型(类或抽象类)。
在一个类中实现两个接口
使用单一类或者使用内部类都可以。
interface A {
void f1();
}
interface B {
void f2();
}
class X implements A, B {
public void f1() {
System.out.println("X...f1...");
}
public void f2() {
System.out.println("X...f2...");
}
}
class Y implements A {
public void f1() {
System.out.println("Y...f1...");
}
B makeB {
return new B() {
public void f2() {
System.out.println("X...f2...");
}
}
}
}
public class Parcel9 {
public static void main(String[] args) {
X x = new X();
x.f1();
x.f2();
Y y = new Y();
B b = y.makeb();
y.f1();
b.f2();
}
}
在一个类中实现两个具体类或抽象类(类似于代理)
只能使用内部类才能实现多重继承。
class D {
void f1() {
System.out.println("f1...");
}
}
abstract class E {
abstract void f2();
}
class Z extends D {
E makeE {
return new E() {
void f2() {
System.out.println("f2...");
}
}
}
}
public class Parcel10 {
public static void main(String[] args) {
Z z = new Z();
z.f1();
E e = z.makeE();
e.f2();
}
}
闭包与回调
闭包(closure)是一个可调用的对象,它记录了一些信息,这些信息来自于创建它的作用域。通过这个定义,可以看出内部类是面向对象的闭包,因为它不仅包含外围类对象(创建内部类的作用域信息),还自动拥有一个指向此外部类的引用,在此作用域内,内部类有权操作所有的成员,包括private成员。
回调:对象能够携带一些信息,这些信息允许它在稍后的某个时刻调用初始的对象。Java语言没有包含指针,通过内部类提供闭包的功能是优良的解决方案,比指针更灵活、安全。
interface AAA {
void f();
}
class BBB {
void f() {
System.out.println("B...");
}
}
public class Parcel12 extends BBB implements AAA {
public void f() {
super.f();
System.out.println("P...");
}
public class CCC {
public void f() {
Parcel12.this.f();
}
}
public static class DDD {
public static void main(String[] args) {
Parcel12 p = new Parcel12();
Parcel12.CCC ccc = p.new CCC();
ccc.f();
}
}
}
内部类与控制框架
Light、Water等是外部类的成员,是系统的硬件,使用内部类可以随意的访问外部类的成员。
public abstract class Event {
private long eventTime;
protected final long delayTime;
public Event(long delayTime) {
this.delayTime = delayTime;
start();
}
public void start() {
eventTime = System.nanoTime() >= delayTime;
}
public boolean ready() {
return System.nanoTime() >= eventTime;
}
public abstract void action();
}
public class Controller {
private List<Event> eventList = new ArrayList<Event>();
public void addEvent(Event c) {
eventList.add(c);
}
public void run() {
while (eventList.size() > 0) {
// 使用for (Event e : eventList) 报java.util.ConcurrentModificationException错误
// https://zhuanlan.zhihu.com/p/85359247
for (Event e : new ArrayList<Event>(eventList)) {
if (e.ready()) {
System.out.println(e);
e.action();
eventList.remove(e);
}
}
}
}
}
public class GreenhouseControls extends Controller{
private boolean light = false;
private boolean water = false;
private String thermostat = "Day";
//开灯事件
public class LightOn extends Event{
public LightOn(long delayTime) {
super(delayTime);
}
public void action() {
light = true;
}
public String toString(){
return "Light is on";
}
}
//关灯事件
public class LightOff extends Event{
public LightOff(long delayTime) {
super(delayTime);
}
public void action() {
light = false;
}
public String toString(){
return "Light is off";
}
}
//开水事件
public class WaterOn extends Event{
public WaterOn(long delayTime) {
super(delayTime);
}
public void action() {
water = true;
}
public String toString(){
return "Water is on";
}
}
//关水事件
public class WaterOff extends Event{
public WaterOff(long delayTime) {
super(delayTime);
}
public void action() {
water = false;
}
public String toString(){
return "Water is off";
}
}
//温度调节于晚上的事件
public class ThermostatNight extends Event{
public ThermostatNight(long delayTime) {
super(delayTime);
}
public void action() {
thermostat = "Night";
}
public String toString(){
return "Therostat on night setting";
}
}
//温度调节于白天的事件
public class ThermostatDay extends Event{
public ThermostatDay(long delayTime) {
super(delayTime);
}
public void action() {
thermostat = "Day";
}
public String toString(){
return "Therostat on day setting";
}
}
//响铃事件
public class Bell extends Event{
public Bell(long delayTime) {
super(delayTime);
}
//响完铃之后又把一个new Bell(delayTime)加入都eventList
public void action() {
addEvent(new Bell(delayTime));
}
public String toString(){
return "Bing!";
}
}
//重启事件
public class Restart extends Event{
private Event[] eventList;
public Restart(long delayTime, Event[] eventList) {
super(delayTime);
this.eventList = eventList;
for(int i = 0; i < eventList.length; i++){
addEvent(eventList[i]);
}
}
public void action() {
for(int i = 0; i < eventList.length; i++){
eventList[i].start();//重新启动每个事件。
addEvent(eventList[i]);
}
start();//启动当前Restart事件
addEvent(this);//把当前的Restart事件加入到eventList中,循环启动。
}
public String toString(){
return "Restarting system!";
}
}
//终止事件
public class Terminate extends Event{
public Terminate(long delayTime) {
super(delayTime);
}
public void action() {
System.exit(0);
}
public String toString(){
return "Terminating!";
}
}
}
public class GreenhouseController {
public static void main(String[] args) {
GreenhouseControls gc = new GreenhouseControls();
gc.addEvent(gc.new Bell(900));
Event[] eventList = {
gc.new ThermostatNight(0),
gc.new LightOn(200),
gc.new LightOff(400),
gc.new WaterOn(600),
gc.new WaterOff(800),
gc.new ThermostatDay(1400)
};
gc.addEvent(gc.new Restart(2000, eventList));
if(args.length == 0){
gc.addEvent(gc.new Terminate(5000));
}
gc.run();
}
}
10.9 内部类的继承
内部类中默认包含一个外部类对象作为成员变量,内部类的构造方法中传入外部类对象初始化该成员变量。而在导出类中不再存在可连接的默认对象。因此必须在导出类的构造器中调用外部类.super()才提供了必要的引用。
public class G1 {
public class G2 {
}
}
public class G3 extends G1.G2 {
G3(G1 g1) { // 不能只是传递一个指向外围类对象的引用
g1.super()'
}
public static void main(String[] args) {
G1 g1 = new G1();
G3 g3 = new G3(g1);
}
}
super和this语法小结
super为父类对象,this为当前对象
- super(delayTime); 调用父类的构造方法
- super.f(); 调用父类的方法
- this.delayTime; 当前对象的成员变量
- addEvent(this); 当前对象
- Parcel3.this; 外部类的引用
10.10 内部类可以被覆盖么
如果创建了一个内部类,然后继承其外围类并重新定义此内部类,内部类可以被覆盖么?
当继承某个外围类时,内部类并没有发生什么变化,两个内部类是完全独立的实体,各自在自己的命名空间内。
可以让子类的内部类继承父类的内部类。
10.11 局部内部类
局部内部类和匿名内部类的比较
局部内部类优点:可以得到不止一个内部类的对象。需要重载构造器或需要一个已命名的构造器。
10.12 内部类标识符
https://blog.csdn.net/weixin_36210698/article/details/73550679
局部内部类,编译器会在”$”加上数字编号LocalClass$1FindImpl.class
匿名内部类,编译器简单产一个数字作为其标识符AClass$1.class
如果内部类是嵌套在别的内部类之中,只需直接将名字加在其外围类标识符与“$”的后面Mulit$One$Two$Three.class