将句柄传递给 java 中的子类时,我应该使用声明还是 this() ?
如果我有一个类创建一个新对象并将 this() 的引用传递给该新对象,我应该只使用 this() 还是应该为该类创建一个声明并传递该声明。我似乎无法找到一个明确的例子来说明在将句柄传递给子对象时何时使用每种情况。
public class ThisClass {
ThisClass decHandle;
ThisClass someThing = this;
public String decString = "some string";
SomeOtherClass classObject1 = new SomeOtherClass(this);
SomeOtherClass classObject2 = new SomeOtherClass(decHandle);
SomeOtherClass classObject3 = new SomeOtherClass(someThing);
}
public class SomeOtherClass {
ThisClass decHandle;
public SomeOtherClass (ThisClass handle){
decHandle = handle;
}
String valueForOther = decHandle.someThing;
}
我主要需要知道将句柄从父类传递给子类时哪种是最佳实践。
If i have a class that creates a new object and passes a refrence to this() to that new object, should i just use this() or should i create a declaration to that class and pass the declaration instead. I cant seem to find a clear example of when i would use each case when passing a handle to a child object.
public class ThisClass {
ThisClass decHandle;
ThisClass someThing = this;
public String decString = "some string";
SomeOtherClass classObject1 = new SomeOtherClass(this);
SomeOtherClass classObject2 = new SomeOtherClass(decHandle);
SomeOtherClass classObject3 = new SomeOtherClass(someThing);
}
public class SomeOtherClass {
ThisClass decHandle;
public SomeOtherClass (ThisClass handle){
decHandle = handle;
}
String valueForOther = decHandle.someThing;
}
I mostly need to know which is the best practice when passing handles to a child class from the parent.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里有不少错误。
1) SomeOtherClass 中的最后一条语句应该是
2
) 当你说它
会抛出 NullPointerException 时,因为你还没有构造对象。 “this”指的是当前对象。在您的代码中,没有对象,因此该行将引发异常。这同样适用于下一行。
3) “子”表示该类继承自父类。仅仅因为 SomeOtherClass 引用了 ThisClass,并不意味着 SomeOtherClass 是子类。
4) 让班级成员拥有公众知名度并不是一个好的做法。
好的,这就是你要如何做的。
There are quite a few mistakes here.
1) The last statement in SomeOtherClass should be
instead of
2) When you say
it will throw a NullPointerException, because you have not yet constructed the object. "this" refers to the current object. In your code, there is no object, therefore this line will throw an exception. The same applies to the next line.
3) "Child" means that the class inherits from a parent. Just because SomeOtherClass has a reference to ThisClass, it doesn't mean that SomeOtherClass is a child.
4) It's not a good practice to let a class' members have public visibility.
Ok, so here's how you would do it.
这样做:
创建一个指向
this
的实例变量不会实现任何目标:您只是在复制this
所做的事情。编辑:请记住,变量(除基元之外)都是 Java 中的引用,因此示例中的
this
和someThing
都指向同一个对象。那么将哪一个传递给SomeOtherClass
并不重要;无论哪种方式,它都会收到对同一对象的引用。SomeOtherClass
中的handle
局部变量不知道也不关心它的引用来自哪里。请注意,在您的示例中
ThisClass.decHandle
为 null,因为您尚未为其分配值。 (事实上,由于未初始化的值,ThisClass
可能无法编译,但我还没有测试过)Do this:
Creating an instance variable that points to
this
doesn't achieve anything: You're just duplicating whatthis
does anyway.EDIT: Remember that variables (other than primitives) are all references in Java, so
this
andsomeThing
in your example both point to the same object. It then doesn't matter which one you pass toSomeOtherClass
; it will receive a reference to the same object either way.The
handle
local variable inSomeOtherClass
doesn't know or care where its reference came from.Note that in your example
ThisClass.decHandle
is null because you have not assigned a value to it. (In factThisClass
probably won't compile because of the uninitialized value, but I haven't tested that)最佳实践是在需要传递对“this”对象的引用时使用
this
。这就是它的用途(除其他外)。如果您尝试避免使用它,最终会使您的代码更加冗长且可读性较差和/或在运行时使用更多内存。(将
this
用于其他目的(例如区分字段和局部变量)是有争议的,但很难提出令人信服的理由来证明它是“最佳实践”。 ..无论如何,这是一个不同的问题。)Best practice is to use
this
when you need to pass a reference to "this" object. That's what it is there for (among other things). If you try to avoid using it you end up making your code more verbose and less readable and / or using more memory at runtime.(The use of
this
for other purposes (e.g. to distinguish fields and locals) is debatable, but it is hard to make a convincing case that it is "best practice" ... or not. Anyway, that's a different question.)