如何在 Java 中访问对象的父对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24057273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 09:56:28  来源:igfitidea点击:

How to access an object's parent object in Java?

javaoop

提问by Andreas Hartmann

Have a look at this example:

看看这个例子:

class Parent{
    Child child = new Child();
    Random r = new Random();
}

class Child{

    public Child(){
        //access a method from Random r from here without creating a new Random()
    }
}

How can I access the Random object from within the Child object?

如何从 Child 对象中访问 Random 对象?

采纳答案by dcastro

Have the Parentclass pass its own instance of Randomto the Childclass.

Parent该类将其自己的实例传递RandomChild该类。

class Parent{
    Child child;
    Random r = new Random();

    public Parent()
    {
        child = new Child(r);
    }
}

class Child{    
    public Child(Random r){

    }    
}

Classic Occam's razor.

经典的奥卡姆剃刀。

回答by Suresh Atta

  • Yes, If it is static method , you can do that (Random.methodName()).

  • If it is an instance method, A big Noooooo. You definitely need Randominstance.

  • 是的,如果是静态方法,你可以这样做(Random.methodName())。

  • 如果是实例方法,A big Noooooo。你肯定需要 Random实例。

回答by amnn

It may be the case that if only your Parentclass ever creates instances of Child, for its own internal use, then you could use inner classes, like so:

可能的情况是,如果只有您的Parent类创建了 的实例Child,供其内部使用,那么您可以使用内部类,如下所示:

class Parent {
    Random r = new Random();
    Child child = new Child();

    private class Child {
        Child() {
            Parent.this.r; // Parent's instance of Random
        }
    }
}

There are other reasons why you may want to use inner classes. But, I'm hesitant to suggest them, because requiring access to another class's instance variables is generally not a good practise.

您可能想要使用内部类还有其他原因。但是,我对建议他们犹豫不决,因为要求访问另一个类的实例变量通常不是一个好的做法。

回答by Shail016

First of all your example doesn't demonstrate parent child relationship in java.

首先,您的示例没有在 Java 中演示父子关系。

Its only a class using another type reference.
in this particular case you can only do new Parent().r //depending upon r's visibility.

它只是一个使用另一种类型引用的类。
在这种特殊情况下,您只能执行 new Parent().r //取决于 r 的可见性。

or you can pass the Parent reference to Child (by changing Child's constructor).

或者您可以将 Parent 引用传递给 Child (通过更改 Child 的构造函数)。

class Parent{
    Child child = new Child(this);
    Random r = new Random();
}

class Child {
    public Child(Parent p){
        //access a method from Random r from here without creating a new Random()
        p.r.nextBoolean();
    }
}

In actual inheritance you don't need to do anything, the super class's members are inherited by extending classes. (again available in child class based on their visibility)

在实际继承中你不需要做任何事情,超类的成员是通过扩展类来继承的。(根据可见性再次在子类中可用)

class Parent{
    Child child = new Child();
    Random r = new Random();
}

class Child extends Parent{

    public Child(){
        //access a method from Random r from here without creating a new Random()
        super.r.nextBoolean();
        //or even r.nextBoolean will be same in this case
    }
}

more at : http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

更多信息:http: //docs.oracle.com/javase/tutorial/java/IandI/subclasses.html