Java函数传递对象与C++传递引用

原文地址:http://blog.csdn.net/luoleicn/article/details/6077512
我听说过这么一句话,“Java函数传参都是传递引用。”,貌似还很流行。不知道是不是Java没有指针概念的缘故,总之如果你学过C++,那就要记住,Java对象在函数间的传递不同于C++概念中的传递引用,如果不懂C++,无视这篇blog。不想纠结于概念,看例子。

 

Java代码:

 

view plain copy to clipboard print ?
  1. public class Test {
  2. int a;
  3. public Test()
  4. {
  5. a = 0;
  6. }
  7. public Test(int a)
  8. {
  9. this.a = a;
  10. }
  11. public void setA(int a)
  12. {
  13. this.a = a;
  14. }
  15. public int getA()
  16. {
  17. return this.a;
  18. }
  19. public static void test(Test passA) {
  20. passA = new Test(99);
  21. }
  22. public static void main(String[] args) {
  23. Test t = new Test();
  24. t.setA(100);
  25. System.out.println(t.getA());
  26. test(t);
  27. System.out.println(t.getA());
  28. }
  29. }
public class Test { int a; public Test() { a = 0; } public Test(int a) { this.a = a; } public void setA(int a) { this.a = a; } public int getA() { return this.a; } public static void test(Test passA) { passA = new Test(99); } public static void main(String[] args) { Test t = new Test(); t.setA(100); System.out.println(t.getA()); test(t); System.out.println(t.getA()); } }

 

运行输出为:

 

100

100

可见虽然在test里为passA赋值了一个新的对象,但是函数运行完后,显然对象并没有发生变化。
第二段Java例子:
view plain copy to clipboard print ?
  1. public class Test {
  2. int a;
  3. public Test()
  4. {
  5. a = 0;
  6. }
  7. public Test(int a)
  8. {
  9. this.a = a;
  10. }
  11. public void setA(int a)
  12. {
  13. this.a = a;
  14. }
  15. public int getA()
  16. {
  17. return this.a;
  18. }
  19. public static void test(Test passA) {
  20. passA.setA(99);
  21. }
  22. public static void main(String[] args) {
  23. Test t = new Test();
  24. t.setA(100);
  25. System.out.println(t.getA());
  26. test(t);
  27. System.out.println(t.getA());
  28. }
  29. }
public class Test { int a; public Test() { a = 0; } public Test(int a) { this.a = a; } public void setA(int a) { this.a = a; } public int getA() { return this.a; } public static void test(Test passA) { passA.setA(99); } public static void main(String[] args) { Test t = new Test(); t.setA(100); System.out.println(t.getA()); test(t); System.out.println(t.getA()); } }
这次在test里没有改变对象,而是调用对象方法,为成员变量赋值,输出结果为:
100
99
下面看一段C++的传引用例子:

view plain copy to clipboard print ?
  1. #include <iostream>
  2. using namespace std;
  3. class Test
  4. {
  5. private:
  6. int m_a;
  7. public:
  8. Test(int a=0){m_a = a;}
  9. void setA(int a){m_a = a;}
  10. int getA(){return m_a;}
  11. };
  12. void test(Test* & t)
  13. {
  14. t = new Test(99);
  15. }
  16. int main(int args, char** argv)
  17. {
  18. Test* t = new Test();
  19. t->setA(100);
  20. cout << t->getA() << endl;
  21. test(t);
  22. cout << t->getA() << endl;
  23. return 0;
  24. }
#include <iostream> using namespace std; class Test { private: int m_a; public: Test(int a=0){m_a = a;} void setA(int a){m_a = a;} int getA(){return m_a;} }; void test(Test* & t) { t = new Test(99); } int main(int args, char** argv) { Test* t = new Test(); t->setA(100); cout << t->getA() << endl; test(t); cout << t->getA() << endl; return 0; }
在test函数中传递引用,同样是赋予一个新的对象,但是结果不同于Java的结果:
100
99
用C++的思维来理解Java,java中的每一个类Object的实例obj相当于对一个Object类的指针,在函数中直接传递这个指针,可以改变指针指向地址的值,但直接修改指针本身是没有用的,因为指针是传值传递过去的,在函数内保存了这个这个指针的副本。
C++的传引用不同,比如:
void func(T & t)
{
//some code
t = x;
}
实现相当于
void func(T * pt)
{
//some code;
*pt = x;
}

你可能感兴趣的:(Java函数传递对象与C++传递引用)