对象中的自引用是不良代码设计的证据
我在我正在查看的一些代码中遇到了自我引用。
示例
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
是的,在某些情况下,隐式自我引用可能是完全自然的。例如,考虑一个当前仅包含单个元素的循环链表。
但是,拥有一个名为
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.我会对此进行尝试。我猜测该代码的作者不知道当您想从嵌套类访问“外部 this”时可以编写 Classname.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:
没有必要。
自引用的一个完全有效的情况是一个需要某些处理程序的对象。如果对象本身实现了该处理程序接口,则对处理程序的引用就是对同一对象的引用。完全可以,恕我直言。
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.
我想说,自引用是 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.
如果没有更多上下文,代码就不会做任何有用的事情,并且可能会造成混乱。我认为它不符合“设计”或“风格”的资格。
然而,更多的背景可能会说明为什么要这样做。
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.
我想说这是糟糕代码的标志:
this
的含义,所以我看不到需要名为selfReference
的引用,TestObject selfReference = this;
//调用一些函数
selfReference = 一些其他对象; //其中 someOtherObject 也是 TestObject 的实例
//调用 someOtherObjects 上的一些函数
I would say that is a sign of bad code:
this
means so I can't see the need for a reference calledselfReference
TestObject selfReference = this;
//call some functions on this
selfReference = someOtherObject; //where someOtherObject is an instance of TestObject as well
//call some functions on someOtherObjects
selfReference
-- since it can end up not referencing self/this!使用更新的代码示例,您似乎在从匿名内部类引用外部对象时遇到了问题。语法是:
OuterClass.this
在您的情况下:
如果您仅使用
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:
If you just use
this
alone, you are referencing thenew ComponentAdapter()
.Thank you for the additional context.
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.
如果您正在编写一个解析器并且具有包含相同类型的子元素的数据结构,那么您将需要具有同一类的成员变量。
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.
很多时候您想要使用
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 ofthis
though, a simplethis.attribute
will suffice.