内部类

  1. 内部类与向上转型

  下例Parcel4类中,内部类PContents是private,所以除了Parcel4,没人能访问它。PDestination是protected,所以只有Parcel4及其子类、还有与Parcel4同一个包中的类能访问PDestination。即客户端程序员想了解或访问这些成员,要受到限制。  

public interface Contents {
    int value();
}
public interface Destination {
    String readLabel();
}
    class Parcel4 {
    private class PContents implements Contents{
        private int i =11;
        public int value(){ return i;}

    }
    protected class PDestination implements Destination{
        private String label;
        private PDestination(String whereTo){
            label = whereTo;
        }
        public String readLabel(){ return label;}
    }
    public Destination destination(String s){
        return new PDestination(s);
    }
    public  Contents contents(){
        return new PContents();
    }
}
public class TestParcel {
    public static void main(String[] args) {
        Parcel4 p = new Parcel4();
        Contents c = p.contents();
        Destination d = p.destination("Tasmania");
        // Illegal -- can't access private class
        // ! Parcel4.PContents pc = p.new PContents();
    }
}

  2. 匿名内部类

public class Parcel7 {
    public Contents contents(){
        return new Contents() { // insert a class definition
            private int i = 11;
            public int value() { return i;}
        };

        public static void main(String[] args) {
            Parcel7 p =new Parcel7();
            Contents c = p.contents();
        }
    }
}

上述代码等同于下属代码:

public class Parcel7b {
    class MyContents implements Contents{
        private int i = 11;
        public int value(){ return i;}
    }
    public Contents contents(){ return new MyContents();}
    public static void main(String[] args) {
            Parcel7b p =new Parcel7b();
            Contents c = p.contents();
        }
}

  3. 闭包与回调

  闭包(Closure)是一个可调用的对象,它记录了一些信息,这些信息来自于创建它的作用域。内部类不仅包含外围类对象(创建内部类的作用域)的信息,还自动拥有一个指向此外围类对象的引用,在此作用域内,内部类有权操作所有的成员。

  java通过内部类提供闭包的功能。通过回调,对象能够携带一些信息,这些信息允许它在稍后的某个时刻用初始的对象。  

public interface Incrementable {
    void increment();
}
public class Callee1 implements Incrementable{
    private int i = 0;
    public void increment(){
        i++;
        System.out.println(i);
    }
}
public class MyIncrement {
    public void increment(){
        System.out.println("other operation");}
        static void f(MyIncrement mi){ mi.increment(); }
}
public class Callee2 extends MyIncrement{
    private int i = 0;
    public void increment(){
        super.increment();
        i++;
        System.out.println(i);
    }
    private class Closure implements Incrementable{
        public void increment(){
            Callee2.this.increment();
        }
    }
    Incrementable getCallbackReference(){
        return new Closure();
    }
}
public class Caller {
    private Incrementable callbackReference;
    Caller(Incrementable cbh){ callbackReference = cbh; }
    void go(){ callbackReference.increment();}
}
public class Callbacks {
    public static void main(String[] args) {
        Callee1 c1 = new Callee1();
        Callee2 c2 = new Callee2();
        MyIncrement.f(c2);
        Caller caller1 = new Caller(c1);
        Caller caller2 = new Caller(c2.getCallbackReference());
        caller1.go();
        caller1.go();
        caller2.go();
        caller2.go();
    }
}

运行结果:

other operation
1
1
2
other operation
2
other operation
3

 

posted @ 2021-09-27 21:03  慕仙白  阅读(67)  评论(0)    收藏  举报