现在我正在研究一个基本的java程序,它将一些参数带入一杯咖啡的构造函数中。这很容易,但我无法创建一种方法来总结我创造的咖啡杯数量。
到目前为止,这是我创建的UseCoffee类:
public class UsesCoffee{
public static void main(String args[]) {
System.out.println("cups created: " + Coffee.totalCups());
Coffee cup1 = new Coffee(350, "mint", true);
System.out.println("cup1: " + cup1);
Coffee cup2 = new Coffee(500, "mocha", false);
System.out.println("cups created: " + Coffee.totalCups());
System.out.println("cup2: " + cup2);
Coffee cup3 = new Coffee(350, "no flavour used", false);
cup3.doubleSize();
System.out.println("cup3: " + cup3);
Coffee cup4 = new Coffee(-10, "mocha", false);
System.out.println("cup4: " + cup4);
System.out.println("cups created: " + Coffee.totalCups());
if (Coffee.bigger(cup3,cup2))
System.out.println("cup3 is bigger than cup2");
if (Coffee.bigger(cup1,cup2))
System.out.println("cup1 is bigger than cup3");
if (Coffee.bigger(cup1,cup1))
System.out.println("cup1 is bigger than itself");
} // end main
} // end UsesCoffee
这就是我创造的咖啡班:
public class Coffee {
private int coffeeVol;
private String coffeeFlav;
private boolean yesCream;
public Coffee(int volume, String flavour, boolean cream) {
this.coffeeFlav = flavour;
this.coffeeVol = volume;
this.yesCream = cream;
if (volume < 0) {
System.out.println("error: negative size. Defaultsize of 250 ml used");
coffeeVol = 250;
}
}
public String toString() {
return coffeeVol +"ml, " + coffeeFlav + ", " + yesCream;
} // end toString
public static int totalCups() {
//THIS IS WHERE I'M HAVING TROUBLE
}
public int doubleSize() {
coffeeVol = coffeeVol*2;
return coffeeVol;
}
}
有没有办法总结咖啡杯的数量?我真的迷失在这方面,任何帮助都表示赞赏!
答案 0 :(得分:3)
您可以向Coffee
类添加一个静态变量,并在构造函数中递增它。
类似的东西:
public class Coffee {
private static int numberOfCups = 0;
private int coffeeVol;
private String coffeeFlav;
private boolean yesCream;
public Coffee(int volume, String flavour, boolean cream) {
this.coffeeFlav = flavour;
this.coffeeVol = volume;
this.yesCream = cream;
if (volume < 0) {
System.out.println("error: negative size. Defaultsize of 250 ml used");
coffeeVol = 250;
}
numberOfCups++;
}
public String toString() {
return coffeeVol +"ml, " + coffeeFlav + ", " + yesCream;
} // end toString
public static int totalCups() {
return numberOfCups;
}
public int doubleSize() {
coffeeVol = coffeeVol*2;
return coffeeVol;
}
public static boolean bigger(Coffee cup1, Coffee cup2) {
if (cup1.coffeeVol > cup2.coffeeVol) {
return true;
}
return false;
}
}
答案 1 :(得分:1)
你想要做的是创建一个静态字段,并在每次调用构造函数时递增它。
public class Coffee {
private static int totalCups;
// rest of the code ...
public Coffee(int volume, String flavour, boolean cream) {
totalCups++;
// rest of the code...
}
public static int getTotalCups() {
return totalCups;
}
}
// rest of the code ...
您希望将字段和方法设置为静态,因为它将由Coffee的所有实例共享。您希望将totalCups设为私有字段,因为数据封装(您不希望允许某人更改杯子总数,只能通过构造函数进行逻辑修改),然后使用公共getter方法检索它(允许你要运行额外的数据验证)
答案 2 :(得分:1)
您可能希望使用称为初始化块的东西和静态整数字段来保存当前类的实例数。在我看来,这是做你想做的最好的方式。
public class Coffee
{
// Hold the number of instances.
private static int instances;
// This snippet of code will run whenever one of the //
// constructors below is called. All subclasses will //
// automatically inherit this too, by the way. //
{
instances++;
}
// first constructor
public Coffee() {}
// second constructor
public Coffee(int Foo) {}
// third constructor
public Coffee(double Bar) {}
// return the number of instances you have.
public static int totalCups()
{
return instances;
}
}
答案 3 :(得分:1)
如果你想计算#/杯......并且你也想比较每个杯子(“哪个杯子最大”)...那么你真的应该考虑使用{{3 }}
实施例
public class UsesCoffee{
public static void main(String args[]) {
ArrayList<Coffee> cups = new ArrayList<Coffee>();
System.out.println("cups created: " + cups.size());
cups.add(new Coffee(350, "mint", true));
cups.add(new Coffee(500, "mocha", false));
System.out.println("cups created: " + cups.size());
Coffee biggestCup = cups.get(0);
for (Coffee cup : cups) {
if (cup.coffeeVol > biggestCup.coffeeVol)
biggestCup = cup;
}
System.out.println("biggest cup is " + biggestCup.coffeeVol);
}
}