Java中的内部类

一、概念

在类的内部创建的类就是内部类。

package com.ray.ch08;  

public class Test {  

    class Destination {  
    }  

    class Content {  
    }  

}  

上面的Destination和Content就是内部类。

二、为什么需要内部类?

(1)隐藏代码

package com.ray.ch08;  

import com.ray.ch08.Test.Destination;  

public class Test {  

    class Destination {  
    }  

    private class Content {  
    }  

}  

class Ship {  
    // Content content = new Test().new Content();//error  
}  

我们可以使用private来隐藏某些实现类。
当我们需要调用某个类的内部类时,必须先new 外部类,就像下面的Destination类一样。

package com.ray.ch08;  

import com.ray.ch08.Test.Destination;  

public class Test {  

    class Destination {  
    }  

    private class Content {  
    }  

}  

class Ship {  
    Destination destination = new Test().new Destination();  
    // Content content = new Test().new Content();//error  
}  

(2)通过与外部类的通信

package com.ray.ch08;  

public class Test {  
    private int id = 0;  

    class Destination {  
        private void print() {  
            System.out.println(id);// 这里的id是int com.ray.ch08.Test.id  
        }  
    }  

    private class Content {  
    }  

}  
package com.ray.ch08;  

public class Test {  
    private int id = 0;  

    public String name = "aaa";  

    private void say() {  
        System.out.println("Class Test Method say");  
    }  

    public void run() {  
    }  

    class Destination {  
        private void print() {  
            System.out.println(id);// 这里的id是int com.ray.ch08.Test.id  
        }  

        private void say() {  
            say();// Class Test Method say  
            System.out.println("Class Destination Method say");  
        }  

    }  

    private class Content {  
        private String a = name;  

        private void run() {  
            run();// Class Test Method run  
        }  
    }  

}  

上面的例子充分展现了内部类访问外部类的各种权限,从私有域到私有方法再到公有域和方法。

三、内部类和迭代器模式
内部类与迭代器模式之间的关系,其实就是迭代器的实现。

package com.ray.ch08;  

public class Sequence {  
    private Object[] items;  

    private int next = 0;  

    public Sequence(int num) {  
        items = new Object[num];  
    }  

    public void add(Object item) {  
        if(next<items.length){  
            items[next++] = item;  
        }  
    }  

    private class SequenceSelector implements Selector {  
        private int i = 0;  

        @Override  
        public boolean end() {  
            return items.length == i;  
        }  

        @Override  
        public void next() {  
            if (items.length > i) {  
                i++;  
            }  
        }  

        @Override  
        public Object current() {  
            return items[i];  
        }  
    }  

    public Selector getSelector() {  
        return new SequenceSelector();  
    }  

    public static void main(String[] args) {  
        Sequence sequence = new Sequence(10);  
        for (int i = 0; i < 10; i++) {  
            sequence.add(i);  
        }  
        Selector selector = sequence.getSelector();  
        while (!selector.end()) {  
            System.out.println(selector.current());  
            selector.next();  
        }  
    }  

}  

interface Selector {  
    boolean end();  

    void next();  

    Object current();  
}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值