楼上正解,创建的是Person类型的空数组,如果你这样搞(赋初值),就没问题- package cty.show;
- public class Temp {
- public static void main(String[] args) {
- Person[] p = new Person[]{new Person(),new Person()};
- p[0].setX(5);
-
- System.out.println(p[0].getX());
- }
- }
- class Person
- {
- public int x;
- int y;
- public void setX(int x)
- {
- this.x = x;
- }
-
- public int getX()
- {
- return x;
- }
- }
复制代码 |