简体   繁体   English

如何在两个不同的类中使用getter和setter

[英]How to use getters and setters in two different classes

how to use getter setter in two different class 如何在两个不同的类中使用getter setter

Class A{

    int a = 10;
    GetterAndSetter gs = new GetterAndSetter();
    gs.setValue(a);
}


Class GetterAndSetter {
    int a ;
    public void setValue(int a){
    this.a = a;
}

public int getValue(){

return a;

   }

}


class B {

    int c;
    GetterAndSetter gs = new GetterAndSetter();
    c = gs.getValue();
}

While printing c it gives null. 打印时,它给出null。 And tell me if it is valid or not. 并告诉我它是否有效。

Whenever you write this 每当你写这个

GetterAndSetter gs = new GetterAndSetter();

what you're doing is to create a new instance of GetterAndSetter . 你正在做的是创建一个GetterAndSetter的新实例。 Two instances that you create won't have any connection between them. 您创建的两个实例之间不会有任何连接。

Inside class A , you create a new instance, and set its value. 在类A ,您创建一个新实例,并设置其值。 Inside class C , you create a new instance, and read its value. C类中,您创建一个新实例,并读取其值。 But because you've got two different instances, the value you're reading isn't connected with the value you're setting. 但是因为您有两个不同的实例,您正在阅读的值与您设置的值无关。

This is roughly like: 这大致类似于:

  1. I buy an envelope, and put some money inside it. 我买了一个信封,里面装了一些钱。
  2. Later on, I want to get the money back, so I buy a new envelope, and look for the money inside it. 后来,我想收回钱,所以我买了一个新的信封,并寻找里面的钱。

You have to be looking in the same envelope that you put the money in, if you want to find it! 如果你想找到它,你必须看着你把钱存入的信封!

In class A , your code creates a new instance of GetterAndSetter and sets a value to the property. A类中,您的代码创建一个GetterAndSetter的新实例,并为该属性设置一个值。 In class B , however, your code creates again another new instance of GetterAndSetter , then gets the value. 但是,在B类中,您的代码再次创建另一个GetterAndSetter新实例,然后获取该值。

The instances your code works with in classes A and B are not the same - hence you don't obtain the values set in A when trying to get it in B . 您的代码在A类和B类中A的实例不相同 - 因此当您尝试在B获取时,您不会获得A设置的值。 The instance of GetterAndSetter created in B is not used anymore after the code in B exits. 实例GetterAndSetter中创建B未在该代码之后再使用B退出。

To fix this, you need to pass a reference to the GetterAndSetter instance from class A to B . 要解决此问题,您需要GetterAndSetter A类到BGetterAndSetter实例的引用传递给它。 You can do this eg by passing it as a parameter to a method of B , or by creating a new instance of A in B and calling a method that provides an instance of GetterAndSetter . 您可以这样做,例如将其作为参数传递给B的方法,或者通过在B创建A的新实例并调用提供GetterAndSetter实例的GetterAndSetter

An example of the first option (pass as parameter): 第一个选项的示例(作为参数传递):

Class A{
...
 GetterAndSetter createAndSet();
    int a = 10;
    GetterAndSetter gs = new GetterAndSetter();
    gs.setValue(a);
    return gs;
  }
...
}

class B {
...
 void getValueFromGetterAndSetter(GetterAndSetter gs) {
    int c;
    c = gs.getValue();
    ...
 }
...
}

To connect the instances, we of course also need to have another piece of code (assuming instances of A and B exist already): 要连接实例,我们当然还需要另一段代码(假设已存在AB实例):

...
        b.getValueFromGetterAndSetter(a.createAndSet());
...

You have used different reference. 您使用了不同的参考。 You should use same reference so that only you can access the value. 您应该使用相同的引用,以便只有您可以访问该值。

你需要了解oops的基础知识,你已经在A类中创建了一个实例,并且你试图在B类中访问,这只有在你将该对象的引用从A类传递给B时才有可能。在这种情况下你必须有你在类B中的A类中创建的GetterAndSetter实例,而是创建了另一个新实例,它将在内存中创建新引用,类变量a将为null。

In your code, both class A and B create new objects for GetterAndSetter. 在您的代码中,A类和B类都为GetterAndSetter创建新对象。 Hence they are not shared between these classes. 因此,这些类之间不共享它们。 Thats why you are getting null. 这就是为什么你得到null。

I wounder how your code print null for C. I think it would be "0" instead. 我的代码如何为C打印null。我认为它将是“0”。

It is valid, here is what happens: 这是有效的,这是发生的事情:

You create object gs in class A 您在A类中创建对象gs

Then you set the value of a of that object to 10 然后将该对象的a值设置为10

You then create another object gs in class B 然后在B类中创建另一个对象gs

Last but not least, you ask the object gs from class B what its value for a is. 最后但并非最不重要的是,你从类B中询问对象gs的值是什么。

Guess what, its NULL as you did not set its value anywhere so it wont return one. 猜猜是什么,它的NULL因为你没有在任何地方设置它的值,所以它不会返回一个。

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

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