接口与类中的内部类 [英] Inner class in interface vs in class

查看:135
本文介绍了接口与类中的内部类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两个内部类声明有什么区别?还评论优缺点?

What is the difference between these two innerclass declarations? Also comment on advantages/disadvantages?

案例A:班级内的班级。

case A: class within a class.

public class Levels {   
  static public class Items {
    public String value;
    public String path;

    public String getValue() {
      return value;}
  }
}

和案例B:接口内的类。

and case B: class within interface.

public interface Levels{

  public class Items {
    public String value;
    public String path;

    public String getValue() {
      return value;}
  }
}

进行修正:放置getvalue方法。

Made correction: to placement of getvalue method.

进一步信息:
我能够实例化Items类A和B都在另一个没有实现接口AT ALL的类中。

further info: I am able to instantiate Items class in both cases A and B in another class that does not implement interface AT ALL.

public class Z{//NOTE: NO INTERFACE IMPLEMENTED here!!!!
 Levels.Items items = new Levels.Items();
}

由于未实例化接口,接口内的所有元素均可通过没有LEVELS接口实例化的点符号只是因为你无法实例化一个接口 - 有效地在一个可以渗透静态引用的接口内定义一个类。

Since an interface is not instantiated, all the elements inside an interface are accessible by dot notation without LEVELS interface instantiated simply because you cannot instantiate an interface - effectively making a class defined inside an interface permeable to static reference.

所以说例如B中的Items类不是静止没有意义。由于情况A和B都以相同的方式实例化,我不是在寻找静态或内部或嵌套的语义。停止给我关于语义的答案。我想要编译器,运行时和行为差异/优点,或者如果没有那么说。没有更多的语义答案请!!!!!关于JVM或.NET VM规范的专家请问这个答案问题而不是教科书语义学。

So saying that Items class in case B is not static does not make sense. Since both cases A and B are instantiated the same way, I am not looking for semantics on what is static or inner or nested. Stop giving me answers on semantics. I want the compiler, runtime and behavioural differences/advantages, or if none then say so. No more answers on semantics please!!!!! An expert on JVM or .NET VM specification innards please this answer question rather than text book semanticissiests.

推荐答案

静态内部类主要是类似于顶级类,除了内部类可以访问所有静态变量和封闭类的方法。封闭的类名有效地附加到内部类的包命名空间。通过将类声明为静态内部类,您传达的是,该类与封闭类的上下文不可分割地联系在一起。

Static inner classes are mostly similar to top-level classes, except the inner class has access to all the static variables and methods of the enclosing class. The enclosing class name is effectively appended to the package namespace of the inner class. By declaring a class as a static inner class, you are communicating that the class is somehow inseparably tied to the context of the enclosing class.

非静态内部类较少共同。主要区别在于非静态内部类的实例包含对封闭类的实例的隐式引用,因此可以访问实例变量和封闭类实例的方法。这导致了一些奇怪的实例化习语,例如:

Non-static inner classes are less common. The main difference is that instances of a non-static inner class contain an implicit reference to an instance of the enclosing class, and as a result have access to instance variables and methods of that enclosing class instance. This leads to some odd looking instantiation idioms, for example:

Levels levels = new Levels(); // first need an instance of the enclosing class

// The items object contains an implicit reference to the levels object
Levels.Items items  = levels.new Items(); 

与静态内部类相比,非静态内部类与其封闭类密切相关。它们具有有效的用途(例如,迭代器通常在它们迭代的数据结构的类中实现为非静态内部类)。

Non-static inner classes are much more intimately tied to their enclosing classes than static inner classes. They have valid uses (for example iterators are often implemented as non-static inner classes within the class of the data structure they iterate over).

声明这是一个常见错误当你真正需要静态内部类行为时,一个非静态内部类。

It's a common mistake to declare a non-static inner class when you only really need the static inner class behaviour.

这篇关于接口与类中的内部类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆