对象中的自引用是不良代码设计的证据

发布于 2024-11-06 02:33:06 字数 1022 浏览 0 评论 0原文

我在我正在查看的一些代码中遇到了自我引用。

示例

TestObject selfReference = this;

是否存在需要在对象中进行自引用的好情况?这是糟糕的编码设计或风格的标志吗?

编辑:

这是一个示例,如果我使用 this ,它会抛出错误,但是当使用 selfReference 时,它​​会编译。


public class IFrame extends InternalFrame
{
    public IFrame()
    {
         addComponentListener(new java.awt.event.ComponentAdapter()
        {
            public void componentResized(java.awt.event.ComponentEvent evt) 
            {
                Window.setCurrComponent(this); //compile error
            }
            public void componentMoved(ComponentEvent evt)
            {
                Window.setCurrComponent(selfReference); //compiles correctly
            }
        });
    }
}

public class InternalFrame extends JInternalFrame
{
    protected InternalFrame selfReference = this;
}

public class Window
{
    InternalFrame currFrame;

    public static void setCurrComponent(InternalFrame iFrame)
    {
        currFrame = iFrame
    }
}

I have come across a self reference in some code I was looking at.

Example

TestObject selfReference = this;

Is there ever a good case in which you would need a self reference in an object? Is this a sign of a bad coding design or style?

EDIT:

This is an example of where if I use this it will throw an error, but when using selfReference, it compiles.


public class IFrame extends InternalFrame
{
    public IFrame()
    {
         addComponentListener(new java.awt.event.ComponentAdapter()
        {
            public void componentResized(java.awt.event.ComponentEvent evt) 
            {
                Window.setCurrComponent(this); //compile error
            }
            public void componentMoved(ComponentEvent evt)
            {
                Window.setCurrComponent(selfReference); //compiles correctly
            }
        });
    }
}

public class InternalFrame extends JInternalFrame
{
    protected InternalFrame selfReference = this;
}

public class Window
{
    InternalFrame currFrame;

    public static void setCurrComponent(InternalFrame iFrame)
    {
        currFrame = iFrame
    }
}

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

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

发布评论

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

评论(10

城歌 2024-11-13 02:33:06

是的,在某些情况下,隐式自我引用可能是完全自然的。例如,考虑一个当前仅包含单个元素的循环链表。

但是,拥有一个名为 selfReference 的成员变量根本没有任何意义。

Yes, there are circumstances in which an implicit self-reference may be entirely natural. Consider, for instance, a circular linked list that currently contains only a single element.

However, having a member variable called selfReference doesn't make any sense at all.

oО清风挽发oО 2024-11-13 02:33:06

我会对此进行尝试。我猜测该代码的作者不知道当您想从嵌套类访问“外部 this”时可以编写 Classname.this 。

也就是说,他创建了一个这样的结构:

class Executor {
    public void execute(Example example) {

    }
}

public class Example {

    Example selfReference = this;

    class Nested {
        public void method() {
            //Oh, oh, can't do this: 
            //new Executor().execute(this);
            //It gives:
            //The method execute(Example) in the type Executor is not applicable for the arguments (Example.Nested)

            //How the hell do I invoke the executor method from here?
            //lets do something really odd.
            new Executor().execute(selfReference);

            //This is what he should have done
            new Executor().execute(Example.this);
        }
    }

}

I'll take a stab on this. I'm guessing that the author of that code didn't know that you can write Classname.this when you want to access "the outer this" from a nested classs.

That is, he created a construct like this:

class Executor {
    public void execute(Example example) {

    }
}

public class Example {

    Example selfReference = this;

    class Nested {
        public void method() {
            //Oh, oh, can't do this: 
            //new Executor().execute(this);
            //It gives:
            //The method execute(Example) in the type Executor is not applicable for the arguments (Example.Nested)

            //How the hell do I invoke the executor method from here?
            //lets do something really odd.
            new Executor().execute(selfReference);

            //This is what he should have done
            new Executor().execute(Example.this);
        }
    }

}
ヅ她的身影、若隐若现 2024-11-13 02:33:06

没有必要。

自引用的一个完全有效的情况是一个需要某些处理程序的对象。如果对象本身实现了该处理程序接口,则对处理程序的引用就是对同一对象的引用。完全可以,恕我直言。

Not necessary.

A perfectly valid case for self-reference is an object that needs some handler. If the object implements that handler interface itself, the reference to handler is the reference to the very same object. Totally OK, IMHO.

玩物 2024-11-13 02:33:06

我想说,自引用是 Java 中糟糕代码的标志,特别是如果它的名称类似于“selfReference”,因为 Java 已经有一个名为“this”的标准自引用。在支持闭包的语言中,这是一个不同的故事,因为非 this 自引用可以将“this”对象保留在闭包的范围内。

I would say a self reference a sign of bad code in Java, especially if it's named something like "selfReference," because Java already has a standard self reference named "this". It's a different story in languages that support closures, since a non-this self reference can keep the "this" object in the scope of the closure.

安静被遗忘 2024-11-13 02:33:06

如果没有更多上下文,代码就不会做任何有用的事情,并且可能会造成混乱。我认为它不符合“设计”或“风格”的资格。

然而,更多的背景可能会说明为什么要这样做。

Without more context, the code doesn't do anything useful and is potentially confusing. I don't think it qualifies as "design" or "style".

However more context might show why it is being done.

顾铮苏瑾 2024-11-13 02:33:06

我想说这是糟糕代码的标志:

  • 每个人都知道 this 的含义,所以我看不到需要名为 selfReference 的引用,
  • 除非您打算这样做稍后将该引用更改为其他内容 - 例如

TestObject selfReference = this;
//调用一些函数
selfReference = 一些其他对象; //其中 someOtherObject 也是 TestObject 的实例
//调用 someOtherObjects 上的一些函数

  • ,但是,如果是这种情况,并且引用在某个时刻被重新分配给其他对象,那么它不应该被称为 selfReference - 因为它最终可能不会引用自我/这个!

I would say that is a sign of bad code:

  • everyone knows what this means so I can't see the need for a reference called selfReference
  • unless of course you are planning to change that reference later on to something else -- e.g.

TestObject selfReference = this;
//call some functions on this
selfReference = someOtherObject; //where someOtherObject is an instance of TestObject as well
//call some functions on someOtherObjects

  • however, if that is the case and the reference gets re-assigned to something else at some point then it shouldn't be called selfReference -- since it can end up not referencing self/this!
心舞飞扬 2024-11-13 02:33:06

使用更新的代码示例,您似乎在从匿名内部类引用外部对象时遇到了问题。语法是:

OuterClass.this

在您的情况下:

public void componentResized(java.awt.event.ComponentEvent evt) 
{
    Window.setCurrComponent(IFrame.this); //no more compile error
}

如果您仅使用 this ,那么您将引用 new ComponentAdapter()

感谢您提供额外的背景信息。

With the updated code sample, it looks like you're having trouble referencing the outer object from an anonymous inner class. The syntax is:

OuterClass.this

In your case:

public void componentResized(java.awt.event.ComponentEvent evt) 
{
    Window.setCurrComponent(IFrame.this); //no more compile error
}

If you just use this alone, you are referencing the new ComponentAdapter().

Thank you for the additional context.

数理化全能战士 2024-11-13 02:33:06

Window.setCurrComponent(this); //编译错误
上面语句中的“this”不是 InternalFrame 对象,而是 ComponentAdapter 对象,就像您在匿名内部类中一样。

Window.setCurrComponent(selfReference); //编译正确
如果您想引用 IFrame 类的对象,上面的行是正确的语句。

Window.setCurrComponent(this); //compile error
'this' in above statement is not InternalFrame object its ComponentAdapter object, as you are in Anonymous inner class.

Window.setCurrComponent(selfReference); //compiles correctly
above line is correct statement if you want to reference your IFrame class' object.

你不是我要的菜∠ 2024-11-13 02:33:06

如果您正在编写一个解析器并且具有包含相同类型的子元素的数据结构,那么您将需要具有同一类的成员变量。

If you are writing a parser and have a datastructure which contains child elements of the same type then you would need to have member variables of the same class.

爱的故事 2024-11-13 02:33:06

很多时候您想要使用this。您应该在需要引用它所属的对象的情况下使用。不过,我认为没有任何理由从 this 创建对象,一个简单的 this.attribute 就足够了。

There's plenty of times when you want to use a this. You should in situations where you need to reference the object it belongs to. I don't see any reason to create an object out of this though, a simple this.attribute will suffice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文