简体   繁体   English

从一种方法调用变量到另一种方法

[英]calling variables from one method to another

Alright, this is driving me crazy! 好吧,这让我发疯! I have two methods and two classes. 我有两个方法和两个类。 I am trying to call a variable from method A to method B. Method A resides in Class A, however it extends Class B. Method B is in Class B. I am using private variables with get/set, but I cannot understand why, when the variable is update in method A it is not transferring over to method B, even though the variable resides in Class B. Here is the code: 我正在尝试从方法A到方法B调用变量。方法A驻留在类A中,但是它扩展了类B。方法B在类B中。我正在使用带有get / set的私有变量,但我不明白为什么,当变量在方法A中更新时,即使变量位于类B中,它也不会转移到方法B。以下是代码:

public class A extends class B {
   public double method A () {
     setSales(userInput.nextDouble());
     double mySales = getSales;
System.out.print("my sales is " +mySales+);
    }
}

public class B {
    private double sales;

    void setSales(double sales) {
    this.sales=sales;
    }

    double getSales() {
    return sales;
    }

   public method B () {
      System.out.print(+sales+);
      //printout equals 0 with user input of any number
     }
}

Local Variables Have No Memory 局部变量没有内存

When you exit a method, both the local variables and the parameter variables disappear (although, if you pass an object as a parameter, you usually have access to the object when you exit the method). 退出方法时,局部变量和参数变量都会消失(尽管,如果将对象作为参数传递,则通常在退出方法时可以访问该对象)。 That means if you make a call to testRightTriangle() and run the declarations, it creates new boxes and initializes them. 这意味着,如果调用testRightTriangle()并运行声明,它将创建新框并对其进行初始化。 When you exit, those local variables disappears. 退出时,这些局部变量消失。

When you call it a second time, it recreates the local variables, and reinitializes them. 当您第二次调用它时,它将重新创建局部变量,并将其重新初始化。

Parameters behave somewhat like this. 参数的行为类似于此。 These variables gets initalized to the arguments. 这些变量被初始化为参数。

Only instance variables maintain their value over method calls. 只有实例变量在方法调用中保持其值。 That is, if some instance variable has value of 4 for height, and you change it to 10, then the next time you make a method call, it's still 10. It stays 10 until some method call changes the value. 也就是说,如果某个实例变量的height值为4,并且将其更改为10,那么下次您进行方法调用时,该值仍为10。它保持为10,直到某个方法调用更改了该值。

 double mySales = getSales;

Needs to be 需要是

  double mySales = getSales();

since getSales is a method. 因为getSales是一种方法。

also

this.sales=sales;

needs to be switched if you are trying to use the parameter passed to the method to set the private variable in class B. 如果您尝试使用传递给该方法的参数设置类B中的私有变量,则需要进行切换。

Class A is not Class B all they have in common are the methods and attributes. 类A不是类B,它们的共同点是方法和属性。 Because Class A extends Class B doesn't mean that they share the same memory thus the field they each hold are stored at different memory locations. 因为类A扩展了类B,并不意味着它们共享相同的内存,因此它们各自持有的字段存储在不同的内存位置。 That's why when you update attribute A in Class A it doesn't not transfer over to Class B. If you want to be able to modify the attribute in Class B you might want to add a parameter to method A. 这就是为什么在类A中更新属性A时不会将其转移到类B的原因。如果您希望能够在类B中修改属性,则可能需要向方法A添加参数。

Change your method A to be this instead: 更改方法A改为:

public double method A (Class B instanceToModify) 
 {
     setSales(userInput.nextDouble());
     instanceToModify.mySales = getSales();
     System.out.print("my sales is " +mySales+);
 }

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

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