• 初识Java 9-1 内部类


     

    目录

    创建内部类

    到外部类的链接

    使用.this和.new

    内部类和向上转型

    在方法和作用域中的内部类

    匿名内部类

    嵌套类

    接口中的类

    从多嵌套的内部类中访问外部人员


    本笔记参考自: 《On Java 中文版》


            定义在另一个类中的类称为内部类。利用内部类,将逻辑上存在关联的类组织在一起,并且可以控制一个类在另一个类中的可见性。

    创建内部类

            创建内部类的方式就是把类定义在一个包围它的类中。

    1. public class Parcel_1 {
    2. class Contents {
    3. private int i = 1;
    4. public int value() {
    5. return i;
    6. }
    7. }
    8. class Destination {
    9. private String label;
    10. Destination(String whereTo) {
    11. label = whereTo;
    12. }
    13. String readLabel() {
    14. return label;
    15. }
    16. }
    17. // 内部类的使用看起来和使用其他类没有区别
    18. public void ship(String dest) {
    19. Contents c = new Contents();
    20. Destination d = new Destination(dest);
    21. System.out.println(d.readLabel());
    22. }
    23. public static void main(String[] args) {
    24. Parcel_1 p = new Parcel_1();
    25. p.ship("一串字符串");
    26. }
    27. }

            程序执行,输出:一串字符串

            在上述程序中,ship()方法进行了内部类对象的创建,这与使用普通类并无什么区别。除了这种使用内部类的方式外,在外部类中设置一个方法,用来返回一个指向内部类的引用,这种形式也很常见:

    1. public class Parcel_2 {
    2. class Contents {
    3. private int i = 1;
    4. public int value() {
    5. return i;
    6. }
    7. }
    8. class Destination {
    9. private String label;
    10. Destination(String whereTo) {
    11. label = whereTo;
    12. }
    13. String readLabel() {
    14. return label;
    15. }
    16. }
    17. public Destination toDest(String s) {
    18. return new Destination(s);
    19. }
    20. public Contents toCon() {
    21. return new Contents();
    22. }
    23. public void ship(String dest) {
    24. Contents c = toCon();
    25. Destination d = toDest(dest);
    26. System.out.println(d.readLabel());
    27. }
    28. public static void main(String[] args) {
    29. Parcel_2 p_1 = new Parcel_2();
    30. p_1.ship("第二串字符串");
    31. Parcel_2 p_2 = new Parcel_2();
    32. // 定义指向内部类的引用
    33. Parcel_2.Contents c = p_2.toCon();
    34. Parcel_2.Destination d = p_2.toDest("这是一个输入");
    35. }
    36. }

            在外部类的非静态方法之外的任何地方创建内部类的对象,其对象类型的指定需要遵循以下格式:

    OuterClassName.InnerClassName

    到外部类的链接

        对于一个负责创建内部类对象的特定外围类对象而言,内部类对象会获得一个隐藏的指向外围类的引用。

            当创建一个内部类时,这个内部类的对象中会隐含一个链接,这个链接用于创建该对象的外围对象。通过这一链接,无需任何条件就可以直接访问外围对象的成员。除此之外,内部类还拥有对外围对象所有元素的访问权

    1. interface Selector {
    2. boolean end();
    3. Object current();
    4. void next();
    5. }
    6. public class Sequence {
    7. private Object[] items;
    8. private int next = 0;
    9. public Sequence(int size) {
    10. items = new Object[size];
    11. }
    12. public void add(Object x) {
    13. if (next < items.length)
    14. items[next++] = x;
    15. }
    16. private class SequenceSelector implements Selector {
    17. private int i = 0;
    18. @Override
    19. public boolean end() {
    20. return i == items.length;
    21. }
    22. @Override
    23. public Object current() {
    24. return items[i];
    25. }
    26. @Override
    27. public void next() {
    28. if (i < items.length)
    29. i++;
    30. }
    31. }
    32. public Selector selector() {
    33. return new SequenceSelector();
    34. }
    35. public static void main(String[] args) {
    36. Sequence sequence = new Sequence(10);
    37. for (int i = 0; i < 10; i++)
    38. sequence.add(Integer.toString(i));
    39. Selector selector = sequence.selector();
    40. while (!selector.end()) {
    41. System.out.print(selector.current() + " ");
    42. selector.next();
    43. }
    44. System.out.println();
    45. }
    46. }

            程序执行的结果是:

            通过sequence中的每一个对象,可以使用Selector接口。这就是一个迭代器设计模式的例子。因为Selector是一个接口,其他类可以使用自己的方式去实现这一接口,而其他方法可以通过Selector这个接口去创建更加通用的代码。

            注意,上述程序中,private字段items并不是内部类SequenceSelector的一部分,但是内部类的end()current()next()方法都使用到了该引用。这就是因为内部类可以访问外围对象的所有方法和字段

    使用.this和.new

            要在内部类中生成外部类对象的引用,可以使用 外部类的名字+.this

    1. public class DotThis {
    2. void f() {
    3. System.out.println("这是外部类DoThis的f()");
    4. }
    5. public class Inner {
    6. public DotThis outer() {
    7. return DotThis.this;
    8. // 若直接使用this,得到的是一个Inner类的引用
    9. }
    10. }
    11. public Inner inner() {
    12. return new Inner();
    13. }
    14. public static void main(String[] args) {
    15. DotThis dt = new DotThis();
    16. DotThis.Inner dti = dt.inner();
    17. dti.outer().f();
    18. }
    19. }

            程序执行,输出:这是外部类DoThis的f()

            若要创建内部类的对象,我们还需要使用其外部类的对象。此时会使用到.new语法:

    1. public class DotNew {
    2. public class Inner {
    3. }
    4. public static void main(String[] args) {
    5. DotNew dn = new DotNew();
    6. DotNew.Inner dni = dn.new Inner();
    7. }
    8. }

            通过这种方式,解决了内部类的名字作用域问题。也因此,不需要使用 dn.new DotNew.Inner() 这种更加冗余的方式(不过这种方式也确实不被允许使用)。

            .new的使用例(部分代码重复多次,因此这次放入图片)

            内部类的对象会隐式地连接到用于创建它的外部类对象

        在之后会出现,嵌套类(static修饰的内部类)不需要指向外部类对象的引用。

    内部类和向上转型

            内部类在进行向上转型,特别是转型为接口时有其独特的优势。因为内部类(即接口的实现)对外部而言是不可见、不可用的,这会方便隐藏实现:外部类只会获得一个指向基类或接口的引用。

            还是引用之前的例子,假设现在存在两个接口DestinationContents

            正如图中所示的,这两个接口可以让客户程序员进行使用。若客户程序员得到的是一个指向这些接口(指向基类同理)的引用,那么他们就无法从这个引用中得知其确切的类型:

    1. class Parcel_4 {
    2. private class PContents implements Contents { // 访问权限为private,无法从外部直接访问
    3. private int i = 1;
    4. @Override
    5. public int value() {
    6. return i;
    7. }
    8. }
    9. protected final class PDestination implements Destination {
    10. private String label;
    11. private PDestination(String whereTo) {
    12. label = whereTo;
    13. }
    14. @Override
    15. public String readLabel() {
    16. return label;
    17. }
    18. }
    19. public Destination destination(String s) {
    20. return new PDestination(s);
    21. }
    22. public Contents contents() {
    23. return new PContents();
    24. }
    25. }
    26. public class TestParcel {
    27. public static void main(String[] args) {
    28. Parcel_4 p = new Parcel_4();
    29. Contents c = p.contents();
    30. Destination d = p.destination("这是第四串字符串");
    31. // 注意:不能访问private类
    32. // Parcel_4.PContents pc = p.new PContents();
    33. }
    34. }

            在Parcel_4中,内部类PContentsprivate的,这表示只有Parcel_4有权对其进行访问。另外,PDestinationprotected的,这表示其的访问权限同样是受限的。

        不能向下转型为访问权限是private的内部类(若无继承关系,也无法向下转型为protected的内部类)。

            private内部类为类的设计者提供了一种方式,这种方式可以完全阻止任何与类型有关的编码依赖,并且可以完全隐藏实现细节。

    在方法和作用域中的内部类

            内部类可以在一个方法或是任何一个作用域内创建。有两个理由支持这种做法:

    1. 像上述例子中展示的,需要实现某种接口,以便创建和返回一个引用
    2. 为解决一个复杂问题,在自己的解决方案中创建了一个类用于辅助,但不希望这个类被公开

    局部内部类

            修改之前的例子,现在创建一个局部内部类。这种类是一个完整的类,它存在于一个方法的作用域中:

    1. public class Parcel_5 {
    2. public Destination destination(String s) {
    3. final class PDestination implements Destination {
    4. private String label;
    5. private PDestination(String whereTo) {
    6. label = whereTo;
    7. }
    8. @Override
    9. public String readLabel() {
    10. return label;
    11. }
    12. }
    13. return new PDestination(s);
    14. }
    15. public static void main(String[] args) {
    16. Parcel_5 p = new Parcel_5();
    17. Destination d = p.destination("这也是一个字符串");
    18. }
    19. }

            在上述程序中,PDestination类是destination()方法的一部分,而不是Parcel_5的一部分因此,PDestinationdestination()外是无法访问的。另外,尽管PDestination类在是destination()进行了定义,但即使destination()方法已经返回,PDestination的对象依旧会是合法的

        在同一子目录下的每一个类中,都可以使用类标识符PDestination来命名内部类,这不会产生命名冲突。

            接下来的例子会展示如何如何将内部类嵌入到一个条件判断的作用域中:

    1. public class Parcel_6 {
    2. private void internalTracking(Boolean b) {
    3. if (b) {
    4. class TrackingSlip {
    5. private String id;
    6. TrackingSlip(String s) {
    7. id = s;
    8. }
    9. String getSlip() {
    10. return id;
    11. }
    12. }
    13. TrackingSlip ts = new TrackingSlip("可以使用");
    14. String s = ts.getSlip();
    15. }
    16. // 超出if的作用域,无法使用内部类
    17. // TrackingSlip ts = new TrackingSlip("不能使用");
    18. }
    19. public void track() {
    20. internalTracking(true);
    21. }
    22. public static void main(String[] args) {
    23. Parcel_6 p = new Parcel_6();
    24. p.track();
    25. }
    26. }

            上述程序中,虽然内部类被布置到了if语句中,但这并不表示这个内部类的创建是有条件的,它会与其他代码一起被编译。


    匿名内部类

            一个内部类可以是匿名的:这种类通常会与方法返回值的创建结合在一起,在值被返回之前插入一个类的定义。

    1. public class Parcel_7 {
    2. // Contents是之前声明的接口,它的方法未被定义
    3. public Contents contents() {
    4. return new Contents() { // 在进行返回时,插入类的定义
    5. private int i = 1;
    6. @Override
    7. public int value() {
    8. return i;
    9. }
    10. }; // 必要的分号
    11. }
    12. public static void main(String[] args) {
    13. Parcel_7 p = new Parcel_7();
    14. Contents c = p.contents();
    15. }
    16. }

            这段代码看起来是在准备创建一个Contents的对象,但返回值却被插入了一个类的定义:

    1. return new Contents() {
    2. // ...
    3. };

    这种语法的意思是“创建一个继承自Contents的匿名类的对象”。在这里,通过new表达式返回的引用会被自动向上转型为一个Contents引用。上述的匿名内部类的语法是以下代码的缩写:

    ---

            另外,上面展示的匿名内部类中,Contents是用无参构造器创建的。若基类需要的是一个带有参数的构造器,那么:

            首先,这个匿名内部类的基类构造器需要带有参数:

    1. public class Wrapping { // 基类Wrapping
    2. private int i;
    3. public Wrapping(int x) { // 含参构造器
    4. i = x;
    5. }
    6. public int value() {
    7. return i;
    8. }
    9. }

            尽管Wrapping只是一个带有实现的普通类,但它也是其子类的公共“接口”。

            然后是匿名内部类的创建:

    1. public class Parcel_8 {
    2. public Wrapping Wrapping(int x) {
    3. return new Wrapping(x) { // 需要将合适的参数传递给基类构造器
    4. @Override
    5. public int value() {
    6. return super.value() * 12;
    7. }
    8. }; // 这个分号标记表达式的结束,但它刚好包含了这个匿名类
    9. }
    10. public static void main(String[] args) {
    11. Parcel_8 p = new Parcel_8();
    12. Wrapping w = p.Wrapping(10);
    13. }
    14. }

            上述程序中,return语句末尾的分号标记着表达式的结束,但它并不会标记类体的结束。

    ---

            若正在构建一个匿名类,并且这个匿名类一定需要使用这个匿名类外部定义的对象,此时,编译器会要求被使用的参数引用使用final修饰,或是“实际上的最终变量”(这种变量在初始化后不再改变,因此被视为final)。

    1. public class Parcel_9 {
    2. public Destination destination(final String dest) {
    3. return new Destination() {
    4. private String label = dest;
    5. @Override
    6. public String readLabel() {
    7. return label;
    8. }
    9. };
    10. }
    11. public static void main(String[] args) {
    12. Parcel_9 p = new Parcel_9();
    13. Destination d = p.destination("这里是Parcel_9");
    14. }
    15. }

            在上述程序中,方法destination()的参数可以不用加上final,但通常会把final写上作为提示。

    ---

            由于匿名类没有名字,所以也不可能有命名的构造器。但如果我们必须对匿名类执行某个类似于构造器的动作,这应该怎么办?借助实例初始化,就可以在效果上为匿名内部类创建一个构造器:

    1. abstract class Base {
    2. Base(int i) {
    3. System.out.println("这是Base的构造器,i = " + i);
    4. }
    5. public abstract void f();
    6. }
    7. public class AnonymousConstructor {
    8. public static Base getBase(int i) {
    9. return new Base(i) {
    10. { // 进行实例初始化
    11. System.out.println("内部类的实例初始化");
    12. }
    13. @Override
    14. public void f() {
    15. System.out.println("匿名类的f()");
    16. }
    17. };
    18. }
    19. public static void main(String[] args) {
    20. Base base = getBase(10);
    21. base.f();
    22. }
    23. }

            程序执行的结果是:

            在这里,传入匿名类的变量i并不一定需要是最终变量,尽管i被传入匿名类的基类构造器,但匿名类内部没有直接使用到它。而下方的程序中,由于匿名类使用了参数,所以被使用的参数必须是最终变量:

    1. public class Parcel_10 {
    2. public Destination destination(final String dest, final float price) {
    3. return new Destination() {
    4. private int cost;
    5. {// 为每个对象执行实例初始化
    6. cost = Math.round(price);
    7. if (cost > 100)
    8. System.out.println("太贵了吧!");
    9. }
    10. private String label = dest;
    11. @Override
    12. public String readLabel() {
    13. return label;
    14. }
    15. };
    16. }
    17. public static void main(String[] args) {
    18. Parcel_10 p = new Parcel_10();
    19. Destination d = p.destination("买什么好呢?", 120);
    20. }
    21. }

            实例初始化操作中包含了一段if语句,这段if语句不能作为字段初始化的一部分来执行。在效果上,实例初始化部分就是匿名内部类的构造器。但因为我们无法重载实例初始化的部分,所以只能有一个这样的构造器。

        与普通的继承相比,匿名构造器只能扩展一个类,或是实现一个接口,且二者不能兼得。

    嵌套类

            将内部类设置为static的,这就变成了嵌套类。这种类不同与普通的匿名类:

    1. 不需要一个外部类对象来创建嵌套类对象;
    2. 无法从嵌套类对象内部访问非static的外部类对象。

            除此之外,嵌套类内部还能存放其他嵌套类,或者static数据及static字段。这些是普通内部类无法做到的:

    1. public class Parcel_11 {
    2. // 嵌套类:带有static的内部类
    3. private static class ParceContents implements Contents {
    4. private int i = 1;
    5. @Override
    6. public int value() {
    7. return i;
    8. }
    9. }
    10. protected static final class ParceDestination implements Destination {
    11. private String label;
    12. private ParceDestination(String whereTo) {
    13. label = whereTo;
    14. }
    15. @Override
    16. public String readLabel() {
    17. return label;
    18. }
    19. // 嵌套类可以包含其他静态元素
    20. public static void f() {
    21. }
    22. static int x = 10;
    23. static class AnotherLevel {
    24. public static void f() {
    25. }
    26. static int x = 10;
    27. }
    28. }
    29. public static Destination destination(String s) {
    30. return new ParceDestination(s);
    31. }
    32. public static Contents contents() {
    33. return new ParceContents();
    34. }
    35. public static void main(String[] args) {
    36. Contents c = contents();
    37. Destination d = destination("不知道写什么,随便写点");
    38. }
    39. }

            普通内部类(即非static的)可以使用特殊的this引用创建向外部类对象的连接。而嵌套类没有特殊的this引用,这使得它和static方法类似。

    接口中的类

            嵌套类可以是接口的一部分,因为类是static的,所以被嵌套的类只是被放到了这个接口的命名空间里。甚至于,可以在嵌套类中实现包围它的接口:

    1. public interface ClassInterface {
    2. void howdy();
    3. class Test implements ClassInterface {
    4. @Override
    5. public void howdy() {
    6. System.out.println("在嵌套类内部实现了外围接口的方法");
    7. }
    8. }
    9. public static void main(String[] args) {
    10. new Test().howdy();
    11. }
    12. }

            程序执行的结果是:

            当需要创建一个接口的所有不同实现的公用代码时,一个嵌套在接口中的类会很有用。

        有时,为了测试一个独立的类,会用到一个单独的main()。这种main()就可以被放入到嵌套类中,在交付产品时将其删去即可。


    从多嵌套的内部类中访问外部人员

            一个类被嵌套了多少层都不重要,因为它可以透明地访问包含它的所有类的所有成员:

    1. class MNA {
    2. private void f() {
    3. }
    4. class A {
    5. private void g() {
    6. }
    7. public class B {
    8. void h() {
    9. g();
    10. f();
    11. }
    12. }
    13. }
    14. }
    15. public class MultiNestingAcess {
    16. public static void main(String[] args) {
    17. MNA mna = new MNA();
    18. MNA.A mnaa = mna.new A();
    19. MNA.A.B mnaab = mnaa.new B();
    20. mnaab.h();
    21. }
    22. }

            不需要在调用构造器时限定类的名字,因为.new语法会寻找到正确的作用域。

  • 相关阅读:
    2023 年如何学习编程
    excel可视化
    跨站脚本攻击(XSS)以及如何防止它?
    RTSP协议抓包及讲解
    为什么macbook不能删除u盘里东西?苹果电脑如何删除u盘文件
    在线预览excel,luckysheet在vue项目中的使用
    量子计算在科技浪潮中的引领作用
    深入解析:如何在遍历List时安全地删除元素
    1-Redis架构设计到使用场景-四种部署运行模式(上)
    SkeyeGisMap地图扩展(五) 自定义形状
  • 原文地址:https://blog.csdn.net/w_pab/article/details/132897180