首页 > 试题广场 >

阅读以下程序,写出输出结果。

[问答题]

阅读以下程序,写出输出结果。

class Swap {
  void sw1 (Person x, Person y){
      int a = y.id;
      y.id = x.id;
      x.id = a;
  }
  void sw2 (Person x, Person y){
      Person e;
 
      e = x;
      y = x;
      x = e;
  }
}
 
public class Person {
  int id = 0;
  Person (int id) {
    this.id = id;
  }
 
  public static void main(String[] args) {
Swap cid = new Swap ();
 
Person p1 = new Person (10);
Person p2 = new Person (11);
Person p3 = new Person (12);
Person p4 = new Person (13);
 
cid.sw1(p1,p2);
cid.sw2(p3,p4);
 
    System.out.println("p1: " + p1.id + "p2: " + p2.id);
    System.out.println("p3: " + p3.id + "p4: " + p4.id);
  }
}

p1: 11p2: 10

p3: 12p4: 13

发表于 2017-05-17 16:37:37 回复(0)