简体   繁体   English

从作为第一个对象中的字段的对象访问对象的字段

[英]Accessing fields of an object from the object that is a field in the first object

I was wondering if it is possible in java to access the fields of the object from one of its own fields that is an object itself. 我想知道在java中是否有可能从它自己的一个对象本身的字段中访问该对象的字段。 Let me present a sample code: 让我提供一个示例代码:

Suppose I have a class (I am condensing the code here, so I know the presented code will not compile) 假设我有一个类(我在这里压缩代码,所以我知道所呈现的代码将无法编译)

public class Driver{
// constructor
public Driver(){
}
// method
public double findArea(){
return ???; <<--line #1 in question  How do I get the length and width fields in here?
}
}// end driver class

public class Car{
 public double length; 
 public double width;
 public Driver me;
public Car(){
length=20.5;
width=11.2;
me = new Driver();
}
}

So in the main section, we would have something like 所以在主要部分,我们会有类似的东西

public static void main(String arg[]){

car = new Car();
car.me.findArea();  << ---line #2 in question
}

so in the line #2 in questions, a public method .findArea() would execute which then needs somehow to tap into the calling object's fields and pull the numeric values (line #1 in question). 所以在问题的第2行中,会执行一个公共方法.findArea(),然后需要以某种方式接入调用对象的字段并拉出数值(相关的第1行)。

Is it at all possible through Java? 通过Java可以实现吗? If not, what would be the best conceptual solution to this problem? 如果没有,那么这个问题的最佳概念解决方案是什么?

thanks!! 谢谢!!

If I understand your question correctly... 如果我理解你的问题......

No, there's no way of navigating backwards from an instance of Driver to a Car which has a driver field referring to it. 不,没有办法从Driver的实例向后导航到有一个driver字段引用它的Car After all, there could be several such Car instances, or none at all. 毕竟,可能有几个这样的Car实例,或者根本没有。

If you want the Driver to know which Car it's part of, you'll need to have a field in Driver to represent that information, and then set it by (say) passing in this from Car to the Driver constructor. 如果您希望Driver知道它所属的Car ,您需要在Driver有一个字段来表示该信息,然后通过(例如)将thisCar传递给Driver构造函数来设置它。 Be careful though - allowing this to escape within a constructor (you call the Driver constructor from the Car constructor) can be dangerous, as you're advertising the existence of an object before it's fully initialized. 但要小心 - 允许this 构造函数中转义(从Car构造函数中调用Driver构造函数)可能很危险,因为在完全初始化之前,您正在宣传对象的存在。

You need to have a reference to the first object. 您需要引用第一个对象。 You could do something like: 你可以这样做:

public class Driver {

    private Car car;

    public Driver(Car car){
        this.car = car;
    }

    public double findArea(){
        return car.getLength() * car.getWidth();
    }

}

public class Car {

    public double length; 
    public double width;
    public Driver me;

    public Car(){
        length=20.5;
        width=11.2;
        me = new Driver(this);
    }

    // getters and setters

}

Other's have already answered your question. 其他人已经回答了你的问题。 I would just like to point out that public data members are considered a Bad Thing(TM) in Object Oriented programming. 我想指出public数据成员被认为是面向对象编程中的Bad Thing(TM)。 As Francisco illustrates, you should use getters and setters instead and change the member variables themselves to private access. 正如Francisco所示,您应该使用getter和setter,并将成员变量本身更改为private访问。

With that said, I believe that your design is not the best. 话虽如此,我相信你的设计不是最好的。 Why does the Driver calculate the area of the Car? 为什么司机计算汽车的面积? Shouldn't the Car be responsible for calculating its own area, especially since it already knows its width and length? 汽车是否应该负责计算自己的区域,特别是因为它已经知道它的宽度和长度? If you designed the code this way, you would completely erase the issue that causes the problem in your original question. 如果您以这种方式设计代码,则可以完全删除导致原始问题出现问题的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM