将句柄传递给 java 中的子类时,我应该使用声明还是 this() ?

发布于 2025-01-04 21:00:52 字数 685 浏览 0 评论 0原文

如果我有一个类创建一个新对象并将 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 技术交流群。

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

发布评论

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

评论(3

西瓜 2025-01-11 21:00:52

这里有不少错误。

1) SomeOtherClass 中的最后一条语句应该是

String valueForOther = decHandle.decString;

2

String valueForOther = decHandle.someThing;

) 当你说它

SomeOtherClass classObject1 = new SomeOtherClass(this);

会抛出 NullPointerException 时,因为你还没有构造对象。 “this”指的是当前对象。在您的代码中,没有对象,因此该行将引发异常。这同样适用于下一行。

3) “子”表示该类继承自父类。仅仅因为 SomeOtherClass 引用了 ThisClass,并不意味着 SomeOtherClass 是子类。

4) 让班级成员拥有公众知名度并不是一个好的做法。

好的,这就是你要如何做的。

public class ThisClass 
{
    private String decString = "some string";
    private SomeOtherClass someOtherClass;

    public ThisClass()
    {
        someOtherClass = new SomeOtherClass(this);
    }

    public String getDecString()
    {
        return decString;   
    }
}

public class SomeOtherClass 
{

    private ThisClass decHandle;
    private String valueForOther;

    public SomeOtherClass (ThisClass handle)
    {
        decHandle = handle;
        valueForOther = decHandle.getDecString(); 
    }  
}  

There are quite a few mistakes here.

1) The last statement in SomeOtherClass should be

String valueForOther = decHandle.decString;

instead of

String valueForOther = decHandle.someThing;

2) When you say

SomeOtherClass classObject1 = new SomeOtherClass(this);

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.

public class ThisClass 
{
    private String decString = "some string";
    private SomeOtherClass someOtherClass;

    public ThisClass()
    {
        someOtherClass = new SomeOtherClass(this);
    }

    public String getDecString()
    {
        return decString;   
    }
}

public class SomeOtherClass 
{

    private ThisClass decHandle;
    private String valueForOther;

    public SomeOtherClass (ThisClass handle)
    {
        decHandle = handle;
        valueForOther = decHandle.getDecString(); 
    }  
}  
若相惜即相离 2025-01-11 21:00:52

这样做:

SomeOtherClass classObject1 = new SomeOtherClass(this);

创建一个指向 this 的实例变量不会实现任何目标:您只是在复制 this 所做的事情。

编辑:请记住,变量(除基元之外)都是 Java 中的引用,因此示例中的 thissomeThing 都指向同一个对象。那么将哪一个传递给 SomeOtherClass 并不重要;无论哪种方式,它都会收到对同一对象的引用。

SomeOtherClass 中的 handle 局部变量不知道也不关心它的引用来自哪里。

请注意,在您的示例中 ThisClass.decHandle 为 null,因为您尚未为其分配值。 (事实上​​,由于未初始化的值,ThisClass可能无法编译,但我还没有测试过)

Do this:

SomeOtherClass classObject1 = new SomeOtherClass(this);

Creating an instance variable that points to this doesn't achieve anything: You're just duplicating what this does anyway.

EDIT: Remember that variables (other than primitives) are all references in Java, so this and someThing in your example both point to the same object. It then doesn't matter which one you pass to SomeOtherClass; it will receive a reference to the same object either way.

The handle local variable in SomeOtherClass 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 fact ThisClass probably won't compile because of the uninitialized value, but I haven't tested that)

零度℉ 2025-01-11 21:00:52

最佳实践是在需要传递对“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.)

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