如何实例化n个类?

时间:2018-10-25 18:13:01

标签: java

我正在尝试创建一些类,但是它们的数量由用户确定。 我要上的课:

public class test {
   public class test() {
      int num;
      //etc.
   }
}

主类:

public class main {
   public static void main(String[] args) {
      test class1 = new test();
      test class2 = new test();
      test class3 = new test();
      //etc.
   }
}

问题是,当前用户可以创建的最大类数量取决于我的耐心。

有没有一种方法可以使它更容易/更短?

Ps:我要用于此项目的项目不适用于列表!

3 个答案:

答案 0 :(得分:0)

您可以使用for循环并将对象保存在列表中:

public static void main(String[] args) {
      int n = 10; //a number given 
      List<Test> myObjs = new ArrayList<Test>();
      for(int i = 0, i < n, i ++) {
        Test t = new Test();
        myObjs.add(t);
      }
}

答案 1 :(得分:0)

您可以利用数组(Test[])或列表(List<Test>)或任何其他想要保存Test对象的数据结构。

public class Test {
    private int num;

    public Test(int num) {
        this.num = num;
    }

    @Override
    public String toString() {
        return String.format("Test %d", this.num); // Override the toString() method
    }
}
public class Main {
    public static void main(String[] args) {
        List<Test> tests = new ArrayList<>();
        int n = 5; // Create five tests

        for (int i = 0; i < n; n++) {
            tests.add(new Test(i)); // Instantiate and add tests to list
        }

        for (Test test : tests) {
            System.out.println(test.toString()); // Print the contents i.e. `num`
        }
    }
}

答案 2 :(得分:0)

只使用一个数组

var_type,category,month,numb_ctm,numb_trn,tot_amt_pd
Age Group,G1: 24 to 37 year old,2018-01-01,896,1136,24701
Age Group,G2: 37 to 42 year old,2018-01-01,99,758,41972
Age Group,G2: 43 to 58 year old,2018-01-01,210,2180,44262
Age Group,G4: 59 to 74 year old,2018-01-01,1025,1408,198467
Age Group,G1: 24 to 37 year old,2018-02-01,27,1681,22071
Age Group,G2: 37 to 42 year old,2018-02-01,297,372,74920
Age Group,G2: 43 to 58 year old,2018-02-01,698,137,74063
Age Group,G4: 59 to 74 year old,2018-02-01,1172,2764,41573
Age Group,G1: 24 to 37 year old,2018-03-01,951,428,3584
Age Group,G2: 37 to 42 year old,2018-03-01,734,896,66483
Age Group,G2: 43 to 58 year old,2018-03-01,185,1129,24417
Age Group,G4: 59 to 74 year old,2018-03-01,1501,847,48692
Age Group,G1: 24 to 37 year old,2018-04-01,275,228,55665
Age Group,G2: 37 to 42 year old,2018-04-01,510,1374,33090
Age Group,G2: 43 to 58 year old,2018-04-01,781,986,122279
Age Group,G4: 59 to 74 year old,2018-04-01,0,1396,49184
Age Group,G1: 24 to 37 year old,2018-05-01,240,1438,46586
Age Group,G2: 37 to 42 year old,2018-05-01,504,381,46717
Age Group,G2: 43 to 58 year old,2018-05-01,358,813,132963
Age Group,G4: 59 to 74 year old,2018-05-01,659,1450,32746
Age Group,G1: 24 to 37 year old,2018-06-01,124,1321,3734
Age Group,G2: 37 to 42 year old,2018-06-01,619,30,15391
Age Group,G2: 43 to 58 year old,2018-06-01,195,1265,50682
Age Group,G4: 59 to 74 year old,2018-06-01,489,1987,132992
Gender,F,2018-01-01,2045,5043,151696
Gender,M,2018-01-01,497,2793,50950
Gender,F,2018-02-01,974,205,184919
Gender,M,2018-02-01,2177,3949,159286
Gender,F,2018-03-01,43,46,9828
Gender,M,2018-03-01,927,520,40851
Gender,F,2018-04-01,288,4875,51426
Gender,M,2018-04-01,778,1994,74220
Gender,F,2018-05-01,1915,4693,258544
Gender,M,2018-05-01,1736,3550,17810
Gender,F,2018-06-01,346,4311,103181
Gender,M,2018-06-01,1157,907,83196