博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java内部类基本使用
阅读量:4313 次
发布时间:2019-06-06

本文共 3480 字,大约阅读时间需要 11 分钟。

链接到外部类

  创建内部类时,那个类的对象同时拥有封装对象(封装内部类的外部类)的一个链接,所以内部类可以访问外部类的成员。

  内部类拥有对外部类所有元素的访问权限。
  看如下代码,内部类SSelector访问外部类元素o,而且o是private。

interface Selector{    boolean end();    Object current();    void next();}public class Sequence{    private Object[] o;    private int next = 0;    public Sequence(int size)    {        o = new Object[size];    }    public void add(Object x)    {        if(next < o.length)        {            o[next] = x;            next++;        }    }    private class SSelector implements Selector    {        int i = 0;        public boolean end()        {            return i == o.length;        }        public Object current()        {            return o[i];        }        public void next()        {            if(i < o.length)            {                i++;            }        }    }    public Selector getSelector()    {        return new SSelector();    }    public static void main(String[] args)    {        Sequence s = new Sequence(10);        for(int i = 0; i < 10; i++)        {            s.add(Integer.toString(i));        }        Selector s1 = s.getSelector();        while (!s1.end())        {            System.out.println((String)s1.current());            s1.next();        }    }}

输出结果如下:

0

1
2
3
4
5
6
7
8
9

static内部类

  • 为创建一个static内部类的对象,不需要一个外部类对象。
  • 不能从static内部类的一个对象访问一个外部类对象。
  • 为创建内部类的对象而不需要创建外部类的一个对象,那么可将所有东西设置为static。
abstract class Contents{    abstract public int value();}interface Destination{    String readLabel();}public class Test3{    private static class PContents extends Contents    {        private int i = 11;        public int value()        {            return i;        }    }    protected static class PDestination implements Destination    {        private String label;        private PDestination(String whereTo)        {            label = whereTo;        }        public String readLabel()        {            return label;        }    }    public static Contents cont()    {        return new PContents();    }    public static Destination dest(String s)    {        return new PDestination(s);    }    public static void main(String[] args)    {        Contents c = cont();        Destination d = dest("Wu Han");        System.out.println(c.value());        System.out.println(d.readLabel());    }}

内部类中引用外部类对象

  若想在内部类中生成外部类的句柄,就要用一个.和this来命名外部类。

  如下,第一次输出为Test3中的x,初始值为0,第二次使用内部类中的method方法对外部类x进行修改,使其变为5。

public class Test3{    int x = 0;    public class Test4    {        int x;        public void method()        {            //内部类x            x = 3;            //外部类            Test3.this.x = 5;        }    }    public Test4 test()    {        return new Test4();    }    public static void main(String[] args)    {        Test3 test3 = new Test3();        Test4 test4 = test3.test();        System.out.println(test3.x);        test4.method();        System.out.println(test3.x);    }}

输出结果:

0

5

通过外部类对象引用内部类对象

  通过外部类对象引用加上.和new与该外部类对应的内部类对象,就可以通过外部类对象来引用内部类对象。

  代码如下,整体与上述代码基本相同,就是在获取内部类对象的时候直接使用.new获取。输出结果也是0 5

public class Test3{    int x = 0;    public class Test4    {        int x;        public void method()        {            //内部类x            x = 3;            //外部类            Test3.this.x = 5;        }    }    public static void main(String[] args)    {        Test3 test3 = new Test3();        Test4 test4 = test3.new Test4();        System.out.println(test3.x);        test4.method();        System.out.println(test3.x);    }}

转载于:https://www.cnblogs.com/yangmingyu/p/6921241.html

你可能感兴趣的文章
给button添加UAC的小盾牌图标
查看>>
如何退出 vim
查看>>
Robberies
查看>>
get post 提交
查看>>
R安装
查看>>
JavaScript高级特性-实现继承的七种方式
查看>>
20121016学习笔记四
查看>>
EntityFramework 学习 一 Stored Procedure
查看>>
Sliverlight之 故事板
查看>>
Java 必知必会的 20 种常用类库和 API
查看>>
HDU 1087 Super Jumping! Jumping! Jumping!
查看>>
0007_初始模块和字节码
查看>>
[效率提升]如何管理好你的电脑文件
查看>>
C++实验二
查看>>
Sultan's Dowry Problem - 苏丹新娘问题
查看>>
SharePoint2010 富文本框添加图片功能的扩展
查看>>
零零碎碎的知识
查看>>
UNIX基础--用户和基本账户管理
查看>>
设计模式
查看>>
5.0以上机器XPOSED框架安装流程
查看>>