Think In Java笔记(六)

Inner class 

1.内部类,在一个类中置入另一个类,可以起到隐藏类的作用。通常的内部类设计中,会返回内部类的引用。

Exercise01

package innerclass;

public class Outer {
    class Inner{
        private int step;
        Inner(int i){
            step = i;
        }
        int get_step() {
            return step;
        }
    }
    public Inner get_inner(int i) {
        return new Inner(i);
    }
    
    public static void main(String[] args) {
        Outer o = new Outer();
        o.get_inner(3).get_step();
    }
}

2.内部类可以访问外部类的所有玩意。

Exercise03

package innerclass;

public class Sequence {
    private String ss;
    Sequence(String s){
        ss = s;
    }
    private class display{
        public String toString() {
            return ss;
        }
    }
    public display re_display() {
        return new display();
    }
    public static void main(String[] args) {
        Sequence s = new Sequence("I am your father");
        display d = s.re_display();
        System.out.println(d.toString());
    }
}

3.如果内部类中需要外部类的引用,那么用Outer class.this。如果想要直接在main里创建内部类的对象,必须使用Outer classs.new

4.非内部类不能声明为private或protected。一旦内部类声明为protected,不同包就算继承外部类也无法访问。内外部类可以互相访问private方法以及数据域。

Exercise07

package innerclass;

class EX06{
    private int ex;
    private int get_ex() {
        return ex;
    }
    private void call_inner() {
        Inner i = new Inner();
        i.modi();
    }
    class Inner{
        private void modi() {
            ex = 1;
            System.out.println(get_ex());
        }
    }
    
    public static void main(String[] args) {
        EX06 e =  new EX06();
        e.call_inner();
}}

5.可以在方法中直接置入内部类,不需要单独分成两部分。

package innerclass;

public class EX09 {
    public Inter1 re_inter1() {
        class Inter implements Inter1{
            private int x;
            public void atleast() {
                System.out.println("Inter" + "(" + x + ")");
            }
        }
        return new Inter();
    }
    
    public static void main(String[] args) {
        EX09 e = new EX09();
        Inter1 i = e.re_inter1();
        i.atleast();
    }
}

 6.对象要在匿名内部类使用必须时final,但是如果是构造器内使用则不需要final。只能implement一个接口。不能同时继承一个类和继承一个接口。

Exercise13

package innerclass;

public class Ex13 {
    public Inter1 inter() {
        return new Inter1() {
            private int x;
            public void atleast() {
                System.out.println("Inter");
            }
        };
    }
    
    public static void main(String[] args) {
        Ex13 e = new Ex13();
        Inter1 i = e.inter();
        i.atleast();
    }
}

Exercise15

package innerclass;

public class Ex15 {
    public First f() {
        return new First() {
            {System.out.println("instance initializer");} //must be embraced by braces
        };
    }
    public static void main(String[] args) {
        Ex15 e = new Ex15();
        e.f();
    }
}


class First{
    private int x;
    First(int i){
        x = i;
    }
    First(){
        x = 1;
        System.out.println("no argument");
    }
}

class Second{
    First f() {
        return new First();
    }
}

7.结合以下的例子,体会用匿名函数创建工厂函数。

Exercise16

package innerclass;

public class Ex16 {
    public static void get(WheelFactory factory) {
        Cycle c = factory.getCycle();
        c.wheels();
    }
    public static void main(String[] args) {
        get(Unicle.wheel);
        get(Tricycle.wheel);
        get(Bicycle.wheel);
    }
}

class Unicle implements Cycle{
    public void wheels() {
        System.out.println("Unicle");
    }
    public static WheelFactory wheel = new WheelFactory() {
        public Cycle getCycle() {
            return new Unicle();
        }
    };
}


class Bicycle implements Cycle{
    public void wheels() {
        System.out.println("Bicycle");
    }
    public static WheelFactory wheel = new WheelFactory() {
        public Cycle getCycle() {
            return new Bicycle();
        }
    };
}

class Tricycle implements Cycle{
    public void wheels() {
        System.out.println("Tricycle");
    }
    public static WheelFactory wheel = new WheelFactory() {
        public Cycle getCycle() {
            return new Tricycle();
        }
    };
}

Exercise17 

package innerclass;
import java.util.*;

public class Ex17 {
    public static void get(Factory factory) {
        Toss t = factory.get_Toss();
        t.display();
    }
    public static void main(String[] args) {
        get(Coin.factory);
        get(Dice.factory);
    }
}

class Coin implements Toss{
    private int i;
    Coin(){
        Random rand = new Random();
        i = rand.nextInt(2);
    }
    public void display() {
        switch(i) {
        case 0:System.out.println("you got positive");
        break;
        case 1:System.out.println("you got negetive");
        }
    }
    public static Factory factory = new Factory() {
        public Toss get_Toss() {
            return new Coin();
        }
    };
}

class Dice implements Toss{
    private int i;
    Dice(){
        Random rand = new Random();
        i = rand.nextInt(6);
    }
    public void display() {
        switch(i) {
            case 0:System.out.println("you got 1");
            break;
            case 1:System.out.println("you got 2");
            break;
            case 2:System.out.println("you got 3");
            break;
            case 3:System.out.println("you got 4");
            break;
            case 4:System.out.println("you got 5");
            break;
            case 5:System.out.println("you got 6");
        }
    }
    public static Factory factory = new Factory() {
        public Toss get_Toss() {
            return new Dice();
        }
    };
}

 8.nested class意思即内部类为static,与外部类没有任何联系也不会有外部类的引用。意味着创建内部类的对象不需要外部类,同时,无法访问外部类的非static对象。我们也可以在接口里置入nested class(因为接口默认其中的类是public static)。

Exercise20

package innerclass;

public class Ex20 implements Nested{
    public static void main(String[] args) {
        Test t = new Test(); //static class created without outer class object 
        t.test();
    }
}

Exercise21

package innerclass;

public interface Nested {
    void howdy();
    public static class Test{
        public static void test(Nested n) {
            n.howdy();
            System.out.println("Test.test()");
        }
    }
}

//

package innerclass;

public class Ex20 implements Nested{
    public void howdy() {
        System.out.println("Nested.howdy()");
    }
    public static void main(String[] args) {
        Ex20 e = new Ex20();
        Test.test(e);
    }
}

9.内部类可以拥有多个实例,特别是当你要同时要继承两个非接口类的时候,只能用内部类。

posted @ 2019-08-02 19:14  waynr  阅读(181)  评论(0编辑  收藏  举报