使用用户输入的对象数创建对象

时间:2016-03-17 12:39:47

标签: java arrays class object

我是java的新用户。作为编程练习,我必须制作一个程序 - 询问用户想要创建多少个对象然后创建它们。类还调用了一个打印已创建对象数的类方法。也 编写创建对象的类。类必须能够跟踪创建的对象的数量。类还需要打印对象数的方法。检查已完成的类以获取类和方法的名称。

我试过以下但没有到达任何地方,所以我期待一些帮助:请帮助!!

import java.util.Scanner;

public class NumberOfObjects{
   public static void main(String args[]) {

       System.out.print("How many objects do you want to create:");
       Scanner reader = new Scanner(System.in);
       int amount = reader.nextInt();

       Thing[] things = new Thing[amount];

       for(int i = 0; i<amount; i++) {
           things[i] = new Thing();
       }

       Thing.numberOfObjects();
   }
   class Thing{
       int count;

       public void numberOfObjects(){
           System.out.println(count);

       }


   }
}

3 个答案:

答案 0 :(得分:1)

你忘记了三件事:

1-在创建对象时增加对象的数量。您可以在Thing构造函数中执行此操作。

2-将count变量声明为static,以允许在Thing类型的所有对象之间共享变量。

3 - 将numberOfObjects方法声明为static,因为它是您通过Thing类访问的类方法

试试这个:

import java.util.Scanner;

public class NumberOfObjects{
public static void main(String args[]) {

   System.out.print("How many objects do you want to create:");
   Scanner reader = new Scanner(System.in);
   int amount = reader.nextInt();

   Thing[] things = new Thing[amount];

   for(int i = 0; i<amount; i++) {
       things[i] = new Thing();
   }

   Thing.numberOfObjects();
   }
   class Thing{
   private static int count = 0;

   public Thing(){
   count++;
   }
   public static void numberOfObjects(){
       System.out.println(count);

   }


   }
}

答案 1 :(得分:1)

class Box
{
   //Keep track of all your objects
   Thing[] objs;
   int cursor;

   public Box(int countOfObjects)
   {
         objs = new Thing[countOfObjects];
   }
   //add new object to the array
   public void add(Thing thing)
   {
       objs[cursor++] = thing
   }
   //gets the object
   public Thing getThing(int pos)
   {
        if(pos < 0 || pos >= objs.lenght())
               throw;

        return objs[pos]; 
   }
   //count the objects
   public int numberOfObjects()
   {
       System.out.println(objs.lenght());
       return objs.lenght(); 
   }

   }
}

class Thing()
{
  //any field you need to store
}

你的主要应该是这样的

 public static void main(String args[]) {

   System.out.print("How many objects do you want to create:");
   Scanner reader = new Scanner(System.in);
   int amount = reader.nextInt();

   Box box = new Box(amount);

   for(int i = 0; i<amount; i++) {
       box.Add(new Thing());
   }

   box.numberOfObjects();
}

答案 2 :(得分:0)

将count计为静态并在构造函数中添加count。另一个导入的东西,将Thing类移出NumberOfObjects类,否则,Thing类是一个内部类,您需要先创建NumberOfObjects实例并使用此对象创建Thing实例

import java.util.Scanner;

public class NumberOfObjects {

public static void main(String args[]) {
    System.out.print("How many objects do you want to create:");
    Scanner reader = new Scanner(System.in);
    int amount = reader.nextInt();

    Thing[] things = new Thing[amount];
    for(int i = 0; i<amount; i++) {
        things[i] = new Thing();
    }
    Thing.numberOfObjects();
}

}

class Thing{
    private static int count ;
    public Thing(){
        count++;
    }
    public static void numberOfObjects(){
        System.out.println(count);
    }
}