英文:
How do I properly reference the variable from another method in java?
问题
我不知道在.toString()方法中应该如何引用变量。我一直在尝试不同的方法。它是公开的,所以我不明白为什么没有被共享。
import java.util.Scanner;
class Donation {
public static void main(String[] args) {
Donation uw1 = new Donation("Washington大学", 600.75, true);
Donation uw2 = new Donation("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 组织, double 金额, boolean 税) {
double v = 金额;
String org = 组织;
boolean b = 税;
}
public String toString(){
if (!v){
return "$" + v + ": " + org;
}
else if (b) {
return "* $" + v + ": " + org;
}
else{
return "错误";
}
}
}
英文:
I don't know how I'm supposed to reference the variables in the .toString() method. 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;
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";
}
}
答案1
得分: 2
你在构造函数中设置了局部变量:
public Donation(String organization, double amount, boolean tax) {
double v = amount;
String org = organization;
boolean b = tax;
}
但是这对你来说没有任何好处,因为这些变量只存在于构造函数内部,一旦构造函数结束,这些变量也会随之消失,这是你问题的关键所在。相反地,将这些变量在构造函数之外、类内部声明。
public class Donation {
private double v;
private String org;
private boolean b;
}
然后,在构造函数内部设置这些字段的值,但不要重新声明它们。
public Donation(String organization, double amount, boolean tax) {
v = amount;
org = organization;
b = tax;
}
然后,你可以在你的toString
方法内部使用这些字段所持有的值。
英文:
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.
答案2
得分: 1
你不能在不同方法之间共享变量,但是你可以使用字段来实现这一点。你可以在构造函数中赋值这些字段,以替换你现在在那里定义的变量,然后在 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.
答案3
得分: 0
在函数内部声明的变量称为“局部变量”。它们的生命周期限定于方法的执行过程中。
因此,v
、org
和 b
仅存在于您的 Donation
构造函数中。当您调用 toString 方法时,它们将不再存在。
如果要拥有与对象生命周期相同的变量,您需要在对象上创建一个字段。
class Donation {
private final double v;
public Donation(double value) {
v = value;
}
public String toString() {
return "$" + v;
}
}
v
是私有且不可更改的。这意味着在对象创建后它不能被修改,这样更容易思考对象的行为,而且在类外部无法访问,这意味着您可以更改类而不影响使用它的其他类。
英文:
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. By the time you call toString they no longer exist
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. 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论