简体   繁体   English

OOP / JAVA:将一个对象与另一个对象关联也会改变其属性(类变量)吗?

[英]OOP/JAVA: Does assinging an object to another changes its attributes(class variables) too?

So my question is if an object A has an attribute such as int var; 所以我的问题是对象A是否具有诸如int var;类的属性int var; and object B has an attribute int var2 both of the object are instantiated from the same class using the same constructor. 并且对象B具有属性int var2这两个对象都使用相同的构造函数从相同的类实例化。 For illustrations purposes lets say that var=3 and var2=0 Does assigning object A=B make A.var==0 ? 为了便于说明,可以说var=3var2=0分配对象A=B是否使A.var==0 please do motivate your answer for me. 请为我激励您的答案。

here is more code in case you need more: 这是更多代码,以防您需要更多:

public class CarSize{
    public CarSize(int x)
    {
        this.var=x;
    }
    public void call()
    {

        doubleVar(this);

    }
    private void doubleVar(CarSize cz)
    {
        int vara= cz.getVar()*2;
        CarSize c= new CarSize(vara);
        cz=c;

    }
    public int getVar(){return this.var;}
    private int var;
    public static void main(String args[])
    {
        CarSize cz = new CarSize(3);
        cz.call();
        System.out.println(cz.getVar());
    }

}

ANNOUNCEMENT 公告

people please check the code before attemtpting to explain to me what i already know, thanks a lot for your understanding. 人们请在尝试输入代码之前向我解释我已经知道的内容,非常感谢您的理解。

The problem why your code does not work as expected lies at this short one of code: 您的代码为何无法按预期运行的问题在于以下简短的代码之一:

cz=c;

Since CarSize is a reference type, variables of CarSize store "pointers" to CarSize objects, not the CarSize objects themselves. 由于CarSize是引用类型,变量CarSize店“指针”,以CarSize对象,而不是CarSize对象本身。

When you are passing a car size as a parameter, you are passing a "pointer". 当您传递汽车尺寸作为参数时,您传递的是“指针”。 cz=c makes the pointer stored in cz point to the same object as that stored in c . cz=c使得存储在所述指针cz指向相同的对象,存储在c

Now let's see how you call this method: 现在让我们看看如何调用此方法:

doubleVar(this);

this refers to the cz that is declared in the main method. this是指在main方法中声明的cz Again, this points to a car size object. 同样, this指向汽车尺寸对象。 In the doubleVar method, you made the parameter point to another object, but you only made the parameter point to something else. doubleVar方法中,将参数指向另一个对象,但仅使参数指向其他对象。 this is never changed, neither did its fields. this是永远不会改变的,它的领域也不会改变。

That's why you don't see the values doubling. 这就是为什么看不到值翻倍的原因。

To fix this, don't assign the parameter a new value. 要解决此问题,请不要为参数分配新值。 Instead, change this directly. 而是直接更改this Get rid of the parameter because that is not needed. 删除该参数,因为这不是必需的。

private void doubleVar()
{
    this.var *= 2; // you can actually omit "this.", I put it there just to make it clear for you

}

You can assign a reference to an object to another object, you cannot assign an object to another object. 您可以将对一个对象的引用分配给另一个对象,不能将一个对象分配给另一个对象。 In your case both A and B would reference the same object previously referenced by only B. Object referenced previously by A will be taken by the garbage collector and cease to exist. 在您的情况下,A和B都将引用先前仅由B引用的同一对象。A先前引用的对象将由垃圾收集器获取并不再存在。

In Java, using the = operator with non-primitives results in aliasing. 在Java中,将=运算符与非基元一起使用会导致混淆。 If you have two different objects of the same class, a and b , saying a = b; 如果您有两个相同类的不同对象,则ab表示a = b; makes a the same object as b , and garbage collection will eventually destroy what used to be in a . a相同的对象b ,和垃圾收集将最终破坏了什么曾经是a

So, yes, the field will be the same, but changing a 's value of the field will also change b 's value of the field because a and b are now the same. 所以,是的,本场将是相同的,但改变a “领域的价值也将改变b ”领域的价值,因为ab和现在相同。

public class Test{

    public static void main(String[] args){

    object a = new object(3);
    object b = new object(0);

    a = b;
    System.out.println(a.var);
  }
}


public class object{

  protected int var;
  public object(int x){
     var = x;
  }
}

This is code of the situation you described exactly. 这是您确切描述的情况的代码。 The output here would be "0" because when a programmer defined object is set equal to another object of the same type it overwrites the reference from the original value of 3 to the new value of 0 此处的输出为“ 0”,因为当程序员定义的对象设置为等于另一个相同类型的对象时,它会将引用从原始值3覆盖为新值0。

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

相关问题 将对象强制转换为其父类以避免在Java中使用其属性 - Casting an Object to its father class for avoiding its attributes in Java 将继承类的对象分配给超类的引用 - Assinging object of inherited class to reference of super class 是否对类进行锁定,也会锁定类变量? - java - Does a lock on class, locks class variables too? - java 在Java中对数组进行关联和对象处理,返回null - Assinging and Object to Array in Java, It is returning null OOP - 一个班级如何成为另一个班级? - OOP - How does one class become another? 将 Class 拆分为 Lambda 中的属性 Java 中的表达式 - Splitting Class into its Attributes in Lambda Expression in Java 无法将对象变量传递给java中另一个类的构造函数 - can't pass object variables to the constructor of another class in java 如何从特定的 class 创建 object 并使用 java 中的方法更改其属性 - How do I create an object from a specific class and change its attributes by using methods in java 如何在Java,C ++或任何面向对象语言中使用动态和变量属性时创建类? - How to create a Class when its attributes are dynamic & variable in Java, C++ or any Object-Oriented Language? 什么设计模式适合将属性应用于改变其行为的类? - What design pattern is good for applying attributes to a class that changes its behavior?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM