所以我正在制作一个可以创建和掷骰子的模具类,返回值和大小。我试图弄清楚如何告诉程序有多少已创建,以便我可以根据有多少来做出不同的响应。 IE我希望printDie的响应为Die Value:如果只有一个die,则为5,Die 1 Value:如果有多个,则为5。
到目前为止,这是我的代码。
package com.catalyse.die;
import java.util.Random;
public class Die
{
// instance variables
private int myDieValue;
private int myDieSides;
private Random myRandom;
// Dice Class Constructors
public Die()
{
this.myDieValue = 1;
this.myDieSides = 4;
}
public Die(int numSides)
{
if ((numSides < 4) || (numSides > 100)) {
System.out.println("Error! You cannot have more than 100 sides or less than four!");
System.exit(0);
}
else {
myDieSides = numSides;
}
}
// getter methods
public int getDieSides()
{
return myDieSides;
}
public int getDieValue()
{
return myDieValue;
}
// setter methods
private void setDieSides(int newNumSides)
{
myDieSides = newNumSides;
}
public void rollDie()
{
Random rand = new Random();
int i = (rand.nextInt(myDieSides) + 1);
myDieValue = i;
}
public void printDie(int dieNum)
{
if (dieNum == 1) {
System.out.println("Die Value: "+myDieValue);
}
else {
System.out.println("Die "+dieNum+" Value: "+myDieValue);
}
}
}
答案 0 :(得分:7)
你的类中可以有静态字段,它可以在构造函数中一直递增。它应该是static
的原因是因为static
字段由类的所有实例共享,因此不会为您创建的每个实例创建该字段的本地副本。
private static int counter = 0;
public Die()
{
counter++;
// Other stuffs
}
// Have a getter method for the counter so that you can
// get the count of instances created at any point of time
public static int getCounter() {
return counter;
}
然后你可以在你的调用方法中调用上面的方法,就像这样
void someMethodInAnotherClass() {
int instanceCount = Die.getCounter(); // You need to call static method using the Class name
// other stuffs.
}
答案 1 :(得分:3)
使用静态成员,即“类”变量,而不是“实例”变量:
private static int count = 0;
在构造函数中:
public Die()
{
count++;
this.myDieValue = 1;
this.myDieSides = 4;
}
一个吸气剂:
public static int getCount() {
return count;
}
答案 2 :(得分:0)
使用静态变量
public class Die{
static int dieCount = 0;
public Die(){
dieCount++;
}
}
每次创建Die
对象时,计数都会增加
public static void main(String[] args){
Die die1 = new Die();
Die die2 = new Die();
int count = Die.dieCount;
}
答案 3 :(得分:0)
了解我的应用程序中计算对象的解决方案
import java.util.Map;
import java.util.TreeMap;
public abstract class ObjectCounter {
private static Map<String, Long> classNameCount = new TreeMap<String, Long>();
public ObjectCounter() {
String key = this.getClass().getName();
if (classNameCount.containsKey(key)) {
classNameCount.put(key, classNameCount.get(key) + 1);
} else {
classNameCount.put(key, 1L);
}
}
public static <T extends ObjectCounter> long getCount(Class<T> c) {
String key = c.getName();
if (classNameCount.containsKey(key)) {
return classNameCount.get(key);
} else {
return 0;
}
}
public static long totalObjectsCreated() {
long totalCount = 0;
for (long count : classNameCount.values()) {
totalCount += count;
}
return totalCount;
}
}
现在扩展ObjectCounter类
见下文
package com.omt.factory;
public class Article extends ObjectCounter {
}
现在所有其他类都在扩展文章类
package com.omt.factory;
public class Bio extends Article {
}
现在这是我们的主要课程
package com.omt.factory;
public class Main {
public static void main(String... a) {
Bio b = new Bio();
Bio b1 = new Bio();
Bio b2 = new Bio();
Bio b3 = new Bio();
Bio b4 = new Bio();
com.omt.temp.Bio bio = new com.omt.temp.Bio();
// Total Objects are created
System.out.println("Total Objects Created By Application :" + ObjectCounter.totalObjectsCreated());
// Get Number Of Objects created for class.
System.out.println("[" + com.omt.temp.Bio.class.getName() + "] Objects Created :"
+ ObjectCounter.getCount(com.omt.temp.Bio.class));
System.out.println("[" + Bio.class.getName() + "] Objects Created :" + ObjectCounter.getCount(Bio.class));
System.out.println("[" + Maths.class.getName() + "] Objects Created :" + ObjectCounter.getCount(Maths.class));
}
}