英文:
Multiple seperate objects constructed from same class
问题
初学者 Java 程序员
我创建了一个名为 Car 的类
public class Car {
private static final double MILES_PER_YEAR = 20000;
private static final double GAS_PRICE = 2.50;
private static double pricePerYear;
private static double totalPrice;
public Car(double initialPrice, double milesPerGallon) {
pricePerYear = (MILES_PER_YEAR / milesPerGallon);
totalPrice = initialPrice;
}
然后在我的主函数中,我构造了两个 Car 对象
Car civic = new Car(22000, 35.5);
Car prius = new Car(27135, 55.5);
然而,当我使用 civic.getTotalPrice()
和 prius.getTotalPrice()
来检查这些对象的属性时,它们总是相同的。第二个对象的构造总是覆盖第一个对象,无论我是像上面那样构造它们,然后获取两个价格,它们都会返回 27135。如果我像下面这样定义它们,情况也是一样的。
Car prius = new Car(27135, 55.5);
Car civic = new Car(22000, 35.5);
对于上面的代码段,civic.getTotalPrice()
和 prius.getTotalPrice()
都会返回 22000。
我认为问题与关键字如 public
、private
、static
、void
、final
等有关,因为我还没有完全理解它们的含义,但我困惑的是,如果是类似关键字的问题,代码为什么仍然能够运行。
我的主要目标是比较单独的对象属性,并且如果有助于任何人理解我在做什么,我希望能够单独修改它们。
英文:
Beginner java programmer
I created a class Car
public class Car {
private static final double MILES_PER_YEAR = 20000;
private static final double GAS_PRICE = 2.50;
private static double pricePerYear;
private static double totalPrice;
public Car(double initialPrice, double milesPerGallon) {
pricePerYear = (MILES_PER_YEAR / milesPerGallon);
totalPrice = initialPrice;
}
Then in my main function I construct two Car objects
Car civic = new Car(22000, 35.5);
Car prius = new Car(27135, 55.5);
However, when I check attributes of these objects with civic.getTotalPrice() and prius.getTotalPrice() they're always the same. The second object contruction always overrides the first and when I construct them like I did above ^^ and get both prices, they both return 27135. This happens the same way vice versa if I define them like this
Car prius = new Car(27135, 55.5);
Car civic = new Car(22000, 35.5);
For the segment above, the return for both civic.getTotalPrice() and prius.getTotalPrice() comes out as 22000
I think the issue has something to do with keywords like public, private, static, void, final, etc. as I do not full yunderstand what they mean yes, but I'm confused as to why the code still runs if it is a keyword problem like that.
My main goal is to compare the seperate objects' attributes and modify them individually if that helps anyone understand where I was going with this.
答案1
得分: 1
使成员变量成为非静态的:
public class Car {
private final double MILES_PER_YEAR = 20000;
private final double GAS_PRICE = 2.50;
private double pricePerYear;
private double totalPrice;
public Car(double initialPrice, double milesPerGallon) {
pricePerYear = (MILES_PER_YEAR / milesPerGallon);
totalPrice = initialPrice;
}
}
static
明确地 表示成员不属于特定实例,而是由所有实例共享。这对于诸如 MILES_PER_YEAR
和 GAS_PRICE
这样的值是有意义的:无论您定义什么样的汽车,这些值始终是相同的。
英文:
Make the member variables non-static:
public class Car {
private static final double MILES_PER_YEAR = 20000;
private static final double GAS_PRICE = 2.50;
private double pricePerYear;
private double totalPrice;
public Car(double initialPrice, double milesPerGallon) {
pricePerYear = (MILES_PER_YEAR / milesPerGallon);
totalPrice = initialPrice;
}
}
static
specifically means that members don’t belong to one specific instance and are instead shared by all of them. This makes sense for values such as MILES_PER_YEAR
and GAS_PRICE
: these are always the same, regardless of what car you define.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论