java - Java中如何计算一个对象的实例数?

标签 java object

现在我正在开发一个基本的 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;
    }


}

有没有办法计算咖啡杯的数量?我在这方面确实迷失了方向,非常感谢任何帮助!

最佳答案

您可以向 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;
    }
}

关于java - Java中如何计算一个对象的实例数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28842443/

相关文章:

java - 匿名类与实现接口(interface)的编程约定

java - 为什么在java中不允许将子类对象的子类集合转换为其父类(super class)对象的父类(super class)集合?

javascript - 如果一个键相同,则创建新的对象数组,然后添加这些对象的其他键的值

javascript - 需要使这个对象数组在 javaScript 中可重用

java - 使特定 ListView 行背景不同颜色 - Android?

java - 打印 Action 中定义的变量

Java 规范实现 - 从那里获取正在实现的接口(interface)的导入

javascript - 从对象返回最佳匹配

javascript - JavaScript 中的 obj[name] 和 obj ['name' ] 有什么区别

Java jList1.getSelectedValue() 返回一个对象。我需要所选列表项中的字符串。