【问题标题】:java how to create array/matrix of different objectsjava如何创建不同对象的数组/矩阵
【发布时间】:2010-11-13 20:30:06
【问题描述】:

我有点困惑

我创建了一个名为 person 的类,它有一个 age 和 name 属性(以及 get set 方法)。 然后在另一个类中,我想创建一个 person 数组,其中每个人都有不同的年龄和姓名。 但有些人最终如何以姓氏和年龄告终。 如果我手动创建它们,那没关系,但是使用 for 循环我遇到了这个问题。 我应该怎么做才能得到不同的人?

这是person类的代码:

public class person {
static String name;
static int age;
public person() {
    name="name";
    age=0;
}
public static String getName() {
    return name;
}
public static void setName(String name) {
    person.name = name;
}
public static int getAge() {
    return age;
}
public static void setAge(int age) {
    person.age = age;
}

public static void main(String[] args) {
    // TODO Auto-generated method stub

}

}

这是我要创建数组/矩阵的代码:

  public class array {
 static person[][] a;

 public static void main(String[] args) {
  a=new person[3][3];


  //manual created person
  person first=new person();
  person second=new person();
  person third=new person();
  first.setAge(12);
  first.setName("first");
  second.setAge(20);
  second.setName("second");
  third.setAge(40);
  third.setName("third");


  //automatic (here I get the disired effect)
  for (int i = 0; i < a.length; i++) {
   for (int j = 0; j < a.length; j++) {
    a[i][j]=new person();
    a[i][j].setAge(10+j);
    a[i][j].setName("Alia"+i);
    System.out.println(i+" "+j+" "+a[i][j].getName()+" "+a[i][j].getAge());
   }
  }

//  a[0][0]=first;
//  a[0][1]=second;
//  a[1][2]=third;
//  System.out.println(a[0][0].getName()+" "+a[0][0].getAge());

  //for checking , and it doesnt work anymore
  System.out.println(a[0][0].getName()+" "+a[0][0].getAge());

//  for (int i = 0; i < a.length; i++) {
//   for (int j = 0; j < a.length; j++) {
//    System.out.println(i+" "+j+" "+a[i][j].getName()+" "+a[i][j].getAge());
//   }
//   
//  }
  getname();

 }

 private static void getname() {
  System.err.println("get name function");
  for (int i = 0; i < a.length; i++) {
   for (int j = 0; j < a.length; j++) {
    System.out.println(a[i][j].getName());
   }
  }

 }

}

【问题讨论】:

    标签: java object matrix


    【解决方案1】:

    从 person 属性中删除 static 关键字。如果它是static,它会被所有实例(所有人物对象)使用。

    但我会这样做:

    public class Person {
        public final String name;
        public final int age;
    
        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public String toString() {
            return name + " (" + age + ")";
        }
    
        public static void main(String... args) {
            List<Person> people = new LinkedList<Person>();
            people.add(new Person("David", 28));
            people.add(new Person("Andreas", 27));
    
            System.out.println(people);
        }
    }
    

    【讨论】:

      【解决方案2】:

      是的,您的属性被声明为静态的。静态属性“属于”类,而不是实例,因此所有实例都看到相同的 String 和 int。你应该没问题,只需从除 main() 之外的所有内容中删除静态。然后,new Person() 将为每个人分配新的单独变量。

      【讨论】:

        【解决方案3】:

        问题是静态字段。最后分配给它们的值将反映在所有对象中。

        【讨论】:

        • 谢谢大家,我到处都删除了静态,它工作了。我从来不知道静态和它的作用。现在我的眼睛睁大了一点。再次感谢^^
        猜你喜欢
        • 2017-05-23
        • 1970-01-01
        • 2021-08-16
        • 1970-01-01
        • 2023-03-09
        • 1970-01-01
        • 2014-05-25
        • 2011-02-11
        • 2014-06-17
        相关资源
        最近更新 更多