简体   繁体   English

如何从java中的另一个方法正确引用变量?

[英]How do I properly reference the variable from another method in java?

I don't know how I'm supposed to reference the variables in the .toString() method.我不知道我应该如何引用 .toString() 方法中的变量。 I've been working on ways to do it.我一直在研究如何做到这一点。 It's public so I don't see why it's not being shared.它是公开的,所以我不明白为什么它没有被共享。 import java.util.Scanner;导入 java.util.Scanner;

class Donation {

public static void main(String[] args) {

    Donation uw1 = new Donation("University of Washington", 600.75, true);

    Donation uw2 = new Donation("University of Washington", 40, true);

    Donation sj = new Donation("Snap Judgment", 30, false);

    Donation tal = new Donation("This American Life", 40, true);

    Donation mc = new Donation("Microphone Check", 99.99, false);

    System.out.println(uw1.toString());
    System.out.println(uw2.toString());
    System.out.println(sj.toString());
    System.out.println(tal.toString());
    System.out.println(mc.toString());
}

public Donation(String organization, double amount, boolean tax) {
    double v= amount;
    String org = organization;
    boolean b= tax;

}

public String toString(){
    if (!v){
        return "$"+v+": "+org;
    }
    else if (b) {
        return "* $" + v + ": " + org;
    }
    else{
        return "error";
    }
}

You set local variables in your constructor:您在构造函数中设置局部变量:

public Donation(String organization, double amount, boolean tax) {
    double v= amount;
    String org = organization;
    boolean b= tax;
}

and this does you know good since these variables exist only within the constructor and once the constructor ends, the variables disappear with it, and this is the crux of your problem.你知道吗,因为这些变量只存在于构造函数中,一旦构造函数结束,变量就会随之消失,这就是问题的症结所在。 Instead, declare those variables outside of the constructor and in the class.相反,在构造函数之外和类中声明这些变量。

public class Donation {

    private double v;
    private String org;
    private boolean b;

Then yes, set the fields within the constructor but don't re-declare them.然后是的,在构造函数中设置字段但不要重新声明它们。

public Donation(String organization, double amount, boolean tax) {
    v= amount;
    org = organization;
    b= tax;
}

Then you can use the values held by these fields within your toString method.然后,您可以在 toString 方法中使用这些字段保存的值。

You cannot share variables across methods, you can however use fields to do this.您不能跨方法共享变量,但是您可以使用字段来执行此操作。 You can assign these fields in the constructor, replacing the variables you've defined there now, and then reference these fields in the toString() method.您可以在构造函数中分配这些字段,替换您现在在那里定义的变量,然后在toString()方法中引用这些字段。

Variables declared within functions are 'local variables'.在函数内声明的变量是“局部变量”。 Their lifetime is the execution of the method.它们的生命周期是方法的执行。

So v , org and b only exist in your Donation constructor.所以vorgb只存在于你的Donation构造函数中。 By the time you call toString they no longer exist当您调用 toString 时,它们已不存在

To have variables whose lifetime is the same as the lifetime of your object you need to create a field on your object.要使变量的生命周期与对象的生命周期相同,您需要在对象上创建一个字段。

class Donation {
  private final double v;
  
  public Donation(double value) {
     v = value;
  }

  public toString() {
     return "$" + v;
  }
}

v is private and final. v是私有的和最终的。 That means that it can't be changed after the object is created, which makes it easier to think about how your object behaves, and can't be seen outside your class, which means that you can change your class without affecting the other classes which use it.这意味着在创建对象之后就不能再更改了,这样可以更容易地思考您的对象的行为方式,并且在您的类之外无法看到,这意味着您可以在不影响其他类的情况下更改您的类使用它。

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

相关问题 如何从Java中的另一个方法引用/定义数组? - How can I reference / define an array from another method in java? Java-如何获取从一个类中的方法生成到另一个类中的变量? - Java - How do I get a variable generated from within a method in one class to another class? 我如何通过 ArrayList<string> 在 Java 中从一种方法到另一种方法。 变量未初始化错误</string> - How do I pass an ArrayList<String> from one method to another in Java. Variable not initialised error 我如何从不同的线程Java中引用方法 - How do i reference a method from a different threads java 如何在同一个类(Java)中的另一个方法中引用一个方法中的变量? - How can I reference a variable within a method in another method in the same class (Java)? 如何在Java RMI中使用可序列化对象和回调方法正确模仿传递引用? - How do I properly mimic pass-by-reference in Java RMI with serializable objects and a callback method? 如何从 Java 中的另一个方法访问变量? - How can I access a variable from another method in Java? 如何在另一种方法中使用一种方法中的变量? - How do I use a variable from one method oin another? 如何从另一个类中的 void 方法调用变量 - How do I call a variable from a void method in another class 如何从另一种方法调用局部变量? - How do i call on a local variable from another method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM