我什么时候应该使用嵌套类的示例?

发布于 2024-08-07 03:30:54 字数 134 浏览 3 评论 0原文

请重新标记这个问题以包含相关的语言

所以我的java书有一整章关于嵌套类,但最后指出,你应该只在“建模组合”时真正使用它们关系和实现您想要隐藏的类的内部结构”。因此,让我们讨论何时要使用嵌套类和一些示例。

Please retag this question to include languages to which it is relevant

So my java book had a whole chapter on nested classes, but ended on the note that you should only really use them when it comes to "modeling composition relationships and implementing internals of a class you want to hide". So lets discuss when you would want to use nested classes and some examples.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你怎么敢 2024-08-14 03:30:54

嵌套/内部类只是一个仅在另一个类的上下文中专门使用的类,该类没有自己的类文件。如果它链接到一个实例,它只能在父类实例的上下文中实例化;它可以看到私有数据,或者如果它是静态类,则只能看到私有静态数据。

java 开发者网站有一个嵌套类教程,其中有一个示例:
http://java.sun.com/docs/books/ Tutorial/java/javaOO/nested.html

几个使用示例:

  • 隐藏一个具体的实现
    接口:(

考虑像 Hibernate 这样的工具的数据库会话):假设您有一个 Session 接口,以及一个返回 Session 实例的 SessionFactory。实现 Session 接口的 SessionImpl 具体类可以是知道如何构造和初始化它的工厂的内部类。

  • 通过实施提供信息
    接口:

在 Wicket Web 框架中,每个 GUI 组件都有一个关联的“模型”,其工作是将数据连接到组件。该界面看起来类似于:

public interface IModel extends IDetachable {
 public Object getObject();
 public Object setObject();
}

假设您有一些特殊的逻辑来检索您编写的自定义 GUI 组件的数据。由于没有其他组件以相同的方式检索数据,因此您可以在提供 IModel 的地方使用匿名类来处理数据检索。如果同一个类中还有另一个点需要重用 IModel 实现,则可以将其设为内部类。稍后,如果您在其他地方需要该模型,您可以将其转换为顶级类。

通常,您在需要类定义的情况下使用内部类,但该类仅在父类的上下文中可用或才有意义。

A nested/inner class is just a class that's only ever used specifically in the context of another class, which doesn't have it's own class file. If it's linked to an instance, it can only be instantiated in the context of a parent class instance; it can see private data, or only private static data if it's a static class.

The java developer site has a nested classes tutorial with one example:
http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html

A couple examples of usage:

  • Hide a concrete implementation of an
    interface:

(Thinking of a database session for a tool like Hibernate): Suppose you have a Session interface, and a SessionFactory which returns an instance of a Session. The SessionImpl concrete class that implements the Session interface could be an innner class of the factory that knows how to construct and initialize it.

  • Supply information by implementing an
    interface:

In the Wicket web framework, each GUI component has an associated "model", whose job is to wire data to the component. The interface looks something like:

public interface IModel extends IDetachable {
 public Object getObject();
 public Object setObject();
}

Suppose you have some special logic to retrieve data for a custom GUI component that you've written. Since no other component retrieves data the same way, you could use an anonymous class at the point where the IModel is supplied to take care of the data retrieval. If you have another point in the same class where you need to reuse your IModel implementation, you could make it an inner class. Later, if you need the model elsewhere, you could convert it to a top-level class.

Generally you use an inner class in a situation where you need a class definition, but that class is only usable or only makes sense in the context of the parent class.

往日 2024-08-14 03:30:54

我在现实生活中使用嵌套类是在全局设置对象中。

父类是单例,嵌套类作为设置类别。

  • 设置
    • 文件设置
    • 打印设置
    • 等等。

将内部对象作为单独的类没有任何实际意义,因为它们在设置类范围之外没有用处。

A real life usage i had with nested classes, was in a global settings object.

The parent class was a Singleton, with nested classes as settings categories.

  • Settings
    • File settings
    • Print settings
    • Etc.

There was no real point in making the inner object as separate classes, as their would be no use for them outside the settings class scope.

悲歌长辞 2024-08-14 03:30:54

我使用嵌套类来封装算法,这些算法通常作为带有大量参数的方法来完成。我使用具有原始数据的类,并将算法放入嵌套类中的单独文件中(使用部分关键字)。这样我就可以在算法完成后放置该算法的属性及其(工作)数据。
我知道无需嵌套类就可以轻松完成,但这感觉是对的,因为算法是专门为父类构建的。

   public partial class Network
    {
            partial void initFDLF()
            {
                fdlf=new FDLF(this);
            }

        public FDLF fdlf;
        public class FDLF
        {
            internal bool changed=true;
            internal bool pvchange=true;
            public double epsilon = 0.001;
            public bool fdlfOk=false;
            public void init(){...}
            public void run(){...}
            ...

I use nested classes for encapsulating algorithms that would be usually done as a method with lots of arguments. I use class that has raw data and I put algorithms into separate file in nested class (using partial keyword). That way I can put properties for that algorithm and its (working) data lives after algorithm is done.
I know that can be easily done without nested classes but this feels right because algorithm is purposely built for parent class.

   public partial class Network
    {
            partial void initFDLF()
            {
                fdlf=new FDLF(this);
            }

        public FDLF fdlf;
        public class FDLF
        {
            internal bool changed=true;
            internal bool pvchange=true;
            public double epsilon = 0.001;
            public bool fdlfOk=false;
            public void init(){...}
            public void run(){...}
            ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文