A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 周志龙 中级黑马   /  2013-10-14 18:33  /  902 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

在JAVA中用等号对类对象进行赋值,实际上操作的是对象的地址。
  1. package MyText;  
  2.    
  3. class ClassA  
  4. {  
  5.     int value;  
  6.     public void seta(int value)  
  7.     {  
  8.         this.value = value;  
  9.     }  
  10.     public void show()  
  11.     {  
  12.         System.out.println("the value:" + value);  
  13.     }  
  14. }  
  15. public class MyText {  
  16.     public static void main (String []args)  
  17.     {  
  18.         ClassA a = new ClassA();  
  19.         a.seta(1);  
  20.         a.show();  
  21.         System.out.println("a:" + a);//a的地址  
  22.         ClassA b = new ClassA();  
  23.         b.seta(2);  
  24.         b.show();  
  25.         System.out.println("b:" + b);//b的地址  
  26.         System.out.println("======================");  
  27.         b = a;  
  28.         a.show();  
  29.         b.show();  
  30.         System.out.println("a:" + a + ", b:" + b);  
  31.         b.seta(3);  
  32.         a.show();  
  33.         b.show();  
  34.         System.out.println("a:" + a + ", b:" + b);  
  35.     }  
  36. }
复制代码
运行结果:

the value:1
a:MyText.ClassA@14a55f2
the value:2
b:MyText.ClassA@15093f1
======================
the value:1
the value:1
a:MyText.ClassA@14a55f2, b:MyText.ClassA@14a55f2
the value:3
the value:3
a:MyText.ClassA@14a55f2, b:MyText.ClassA@14a55f2

在java中向函数传入类对象参数实际上操作的也是地址
eg:
  1. package MyText;  
  2.    
  3. class ClassA  
  4. {  
  5.     int value;  
  6.     public void seta(int value)  
  7.     {  
  8.         this.value = value;  
  9.     }  
  10.     public void show()  
  11.     {  
  12.         System.out.println("the value:" + value);  
  13.     }  
  14. }  
  15. public class MyText {  
  16.     public static void showClassA(ClassA a)  
  17.     {  
  18.         a.show();  
  19.         System.out.println("a:" + a);  
  20.         a.seta(5);  
  21.         a.show();  
  22.         System.out.println("a:" + a);  
  23.     }  
  24.     public static void main (String []args)  
  25.     {  
  26.         ClassA a = new ClassA();  
  27.         a.seta(1);  
  28.         a.show();  
  29.         System.out.println("a:" + a);  
  30.         showClassA(a);  
  31.         a.show();  
  32.         System.out.println("a:" + a);  
  33.     }  
  34. }
复制代码
运行结果:
the value:1
a:MyText.ClassA@5e55ab
the value:1
a:MyText.ClassA@5e55ab
the value:5
a:MyText.ClassA@5e55ab
the value:5
a:MyText.ClassA@5e55ab
而在C++中向函数传递类对象参数时,是按值传递的,即实参与形参间进行成员变量赋值操作,而不是地址

eg:
  1. # include <iostream>  
  2. using namespace std;  
  3.    
  4. class ClassA  
  5. {  
  6. private:  
  7.     int value;  
  8. public:  
  9.     void seta(int value)  
  10.     {  
  11.         this->value = value;  
  12.     }  
  13.     void show()  
  14.     {  
  15.         cout<<"the value : "<<value<<endl;  
  16.     }  
  17. };  
  18. void show(ClassA a)  
  19. {  
  20.     a.show();  
  21.     cout<<"a:"<<&a<<endl;  
  22.     a.seta(5);  
  23.     a.show();  
  24. }  
  25. int main ()  
  26. {  
  27.     ClassA a;  
  28.     a.seta(3);  
  29.     a.show();  
  30.     cout<<"a:"<<&a<<endl;  
  31.     show(a);  
  32.     a.show();  
  33. }
复制代码
运行结果:
the value : 3
a:0x22fefc
the value : 3
a:0x22fea0
the value : 5
the value : 3


Process returned 0 (0x0)   execution time : 0.130 s
Press any key to continue.
对于利用等号对对象进行赋值,实际上也是对对象成员的值按值传递,而不是传递地址

eg:
  1. # include <iostream>  
  2. using namespace std;  
  3.    
  4. class ClassA  
  5. {  
  6. private:  
  7.     int value;  
  8. public:  
  9.     void seta(int value)  
  10.     {  
  11.         this->value = value;  
  12.     }  
  13.     void show()  
  14.     {  
  15.         cout<<"the value : "<<value<<endl;  
  16.     }  
  17. };  
  18. int main ()  
  19. {  
  20.     ClassA a;  
  21.     a.seta(3);  
  22.     a.show();  
  23.     cout<<"a:"<<&a<<endl;  
  24.     ClassA b;  
  25.     b.seta(4);  
  26.     b.show();  
  27.     cout<<"b:"<<&b<<endl;  
  28.     b = a;  
  29.     a.show();  
  30.     b.show();  
  31.     cout<<"a:"<<&a<<", b"<<&b<<endl;  
  32.     b.seta(6);  
  33.     a.show();  
  34.     b.show();  
  35.     cout<<"a:"<<&a<<", b"<<&b<<endl;  
  36. }
复制代码
运行结果:

the value : 3
a:0x22fefc
the value : 4
b:0x22fef8
the value : 3
the value : 3
a:0x22fefc, b0x22fef8
the value : 3
the value : 6
a:0x22fefc, b0x22fef8


Process returned 0 (0x0)   execution time : 0.132 s
Press any key to continue.

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马