1 class Person{
 2     private int age;
 3     public void setAge(int para){
 4          age = para;
 5     }
 6     public int getAge(){
 7         return age;
 8     }
 9 }
10 public static void main(String args[]){
11    Person p = new Person();
12    Person q;
13    p.setAge(10);
14    ArrayList<Person> students = new ArrayList<Person>();
15    students.add(p);
16    q = students.get(0);
17    q.setAge(20);
18    System.out.println(students.get(0).getAge());//输出结果为20,因为q未初始化分配内存,对q赋值时,q与students.get(0)使用的是同一块内存,修改q,即修改students
19 }

相关文章:

  • 2022-12-23
  • 2022-03-09
  • 2021-10-07
  • 2021-05-09
  • 2022-12-23
  • 2021-08-27
  • 2022-02-04
猜你喜欢
  • 2021-09-17
  • 2022-12-23
  • 2022-12-23
  • 2021-10-04
  • 2022-12-23
  • 2022-12-23
  • 2022-01-24
相关资源
相似解决方案