inner classes in methods and scopes

本文深入探讨了Java中局部内部类的使用场景与实现原理。解释了局部内部类如何在方法或任意作用域内创建,以及为何选择这种方法:一是为了实现某种接口并返回引用;二是为了解决复杂问题而创建辅助类,但不希望其公开。文章通过具体示例展示了局部内部类的定义和使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Inner classes can be created within a method or even an arbitrary scope. There are two reasons for doing this:

1. We’re implementing an interface of some kind so we can create and return a reference.

2. We’re solving a complicated problem and we create a class to aid in we solution, but we don’t want it publicly available.


local inner class (an entire class within the scope of a method), for example:

// innerclasses/Parcel5.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Nesting a class within a method

public class Parcel5 {
  public Destination destination(String s) {
    final class PDestination implements Destination { // not have an access specifier
      private String label;

      private PDestination(String whereTo) {
        label = whereTo;
      }

      @Override
      public String readLabel() {
        return label;
      }
    }
    return new PDestination(s); // upcasting
  }

  public static void main(String[] args) {
    Parcel5 p = new Parcel5();
    Destination d = p.destination("Tasmania");
  }
}

The upcasting in the return statement means nothing comes out of destination() except a reference to a Destination interface. The fact that the name of the class PDestination is placed inside destination() doesn’t mean PDestination is not a valid object once destination() returns. We can use the class identifier PDestination for an inner class inside each class in the same subdirectory without a name clash.

Then, see how we can nest an inner class within any arbitrary scope:

// innerclasses/Parcel6.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Nesting a class within a scope

public class Parcel6 {
  private void internalTracking(boolean b) {
    if (b) {
      class TrackingSlip { // inner class, after compiled it is Parcel6$1TrackingSlip.class
        private String id;

        TrackingSlip(String s) {
          id = s;
        }

        String getSlip() {
          return id;
        }
      }
      TrackingSlip ts = new TrackingSlip("slip");
      String s = ts.getSlip();
    }
    // Can't use it here! Out of scope:
    // - TrackingSlip ts = new TrackingSlip("x"); // [1]
  }

  public void track() {
    internalTracking(true);
  }

  public static void main(String[] args) {
    Parcel6 p = new Parcel6();
    p.track();
  }
}

[1]'s compile error:

innerclasses/Parcel6.java:25: error: cannot find symbol
    TrackingSlip ts = new TrackingSlip("x");
    ^
  symbol:   class TrackingSlip

references:

1. On Java 8 - Bruce Eckel

2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/innerclasses/Parcel5.java
3. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/innerclasses/Parcel6.java

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值