计算Java中的对象数

时间:2015-05-23 23:48:12

标签: java oop count

我正在创建一个名为Humans and Pets的程序。该程序只打印出一个人的名字列表(在这种情况下,我创建了4个)及其相应的宠物。这是代码:

AmazingPets.java

public class AmazingPets {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        System.out.println("Welcome to Pets and Humans! Created By Marc B.\n____________________________\n");

        Dogs firstDog = new Dogs("Ghost");
        Humans firstName = new Humans("Alex");
        Dogs secondDog = new Dogs("Paperbag");
        Humans secondName = new Humans("Michael");
        Cats firstCat = new Cats("Tom");
        Cats secondCat = new Cats("Mr Furball");
        Humans thirdName = new Humans("Bryan");
        Humans fourthName = new Humans("Julie");
        System.out.printf("%s's dog's name is %s.\n", firstName.getHumanName(), firstDog.getDogName());
        System.out.printf("%s's dog's name is %s.\n", secondName.getHumanName(), secondDog.getDogName());
        System.out.printf("%s's cat's name is %s.\n", thirdName.getHumanName(), firstCat.getCatName());
        System.out.printf("%s's cat's name is %s.\n", fourthName.getHumanName(), secondCat.getCatName());

    }
}

Humans.java

public class Humans {
    private String mHumanName;
    public Humans(String humanName) {
        mHumanName = humanName;
    }
    public String getHumanName() {
        return mHumanName;
    }
}

我想为人类创建一个名为populationCount的类方法,它将返回创建的Humans个实例的总数。然后我想输出结果(使用AmazingPets.java中的Scanner)来获得控制台中的计数。

有人可以建议可能的方法来回报人类的总人数吗?因为我似乎无法在网上找到任何资源。先感谢您。 :)

2 个答案:

答案 0 :(得分:0)

创建一个静态字段private static int humanCount = 0并在构造函数中递增它:

 public class Humans {
    private String mHumanName;
    private static int humanCount = 0;

    public Humans(String humanName) {
        mHumanName = humanName;
        humanCount++;
    }
    public String getHumanName() {
        return mHumanName;
    }
    public static int populationCount() {
        return humanCount;
    }
}

您可以添加finalize()方法并使用它来减少计数。当对象被销毁时它将被调用。

protected void finalize( ) throws Throwable {
    humanCount--;
    super.finalize();
}

答案 1 :(得分:0)

您可以使用此抽象类,以计算继承它的任何类型的对象。答案是基于addy2012的答案(谢谢!):

public abstract class Countable
{
    private static final Map<Class<?>, Integer> sTotalCounts = new HashMap<>();

    public Map<Class<?>, Integer> getCountsMap() {
        return sTotalCounts;
    }

    public int getTotalCount()
    {
        return sTotalCounts.get(this.getClass());
    }

    public Countable()
    {
      int count = 0;

      //Add if it does not exist.
      if(sTotalCounts.containsKey(this.getClass()))
      {
          count = sTotalCounts.get(this.getClass());
      }
      sTotalCounts.put(this.getClass(), ++count);
    }
}

然后,你可以这样做:

public class Dogs   extends Countable {/**/}
public class Cats   extends Countable {/**/}
public class Humans extends Countable {/**/}

然后,您可以实例化任何对象

Dogs   dog   = new Dogs("...");
Dogs   dog2  = new Dogs("...");
Cats   cat   = new Cats("...");
Humans human = new Humans("...");

然后,您可以通过从实例调用getTotalCount方法来获取每个总计数:

System.out.println(dog.getTotalCount());
System.out.println(cat.getTotalCount());
System.out.println(human.getTotalCount());

哪个会给你

2
1
1

重要说明:

1)getTotalCount()通过实例调用(非静态)。这在语义上可能很奇怪,因为你有一个方法返回一个总共实例的结果,所以对它的任何修改都会很好。

2)为了计算不同类型的数量,map get&amp; put操作应用。这些行动有其复杂性,在案件中可能代价高昂。有关此问题的更多信息,请查看this answer。