java中的传值与传reference .
类别: JAVA教程
//: c03:Assignment.java
// Assignment with objects is a bit tricky.
// From \'Thinking in Java, 3rd ed.\' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
// edit by nx.
class Number {
int i;
}
public class Assignment {
public static void main(String[] args) {
Number n1 = new Number();
Number n2 = new Number();
n1.i = 9;
n2.i = 47;
System.out.println("1: n1.i= " + n1.i + ", n2.i= " + n2.i);
n1 = n2;
System.out.println("2: n1.i= " + n1.i + ", n2.i= " + n2.i);
n1.i = 27;
System.out.println("3: n1.i= " + n1.i + ", n2.i= " + n2.i);
}
}
RUN:
1: n1.i= 9, n2.i= 47
2: n1.i= 47, n2.i= 47
3: n1.i= 27, n2.i= 27
// Assignment with objects is a bit tricky.
// From \'Thinking in Java, 3rd ed.\' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
// edit by nx.
class Number {
int i;
}
public class Assignment {
public static void main(String[] args) {
Number n1 = new Number();
Number n2 = new Number();
n1.i = 9;
n2.i = 47;
System.out.println("1: n1.i= " + n1.i + ", n2.i= " + n2.i);
n1 = n2;
System.out.println("2: n1.i= " + n1.i + ", n2.i= " + n2.i);
n1.i = 27;
System.out.println("3: n1.i= " + n1.i + ", n2.i= " + n2.i);
}
}
RUN:
1: n1.i= 9, n2.i= 47
2: n1.i= 47, n2.i= 47
3: n1.i= 27, n2.i= 27
- 上一篇: Java Security Notes (6)
- 下一篇: java大进攻(1)
-= 资 源 教 程 =-
文 章 搜 索