首页 新闻 会员 周边 捐助

一道简单的代码题目

1
[已解决问题] 解决于 2010-09-01 19:27
<ol class="dp-c"> <li class="alt"><span><span><strong>&nbsp;&nbsp; 首先我定义了一个类: &nbsp;&nbsp;</strong></span></span></li> <strong> </strong> <li><span><span class="keyword"><strong>sealed</strong></span><span><strong>&nbsp;&nbsp;</strong></span><span class="keyword"><strong>class</strong></span><span><strong>&nbsp;UserModel &nbsp;&nbsp;</strong></span></span></li> <strong> </strong> <li class="alt"><span><strong>&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;</strong></span></li> <strong> </strong> <li><span><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</strong></span><span class="keyword"><strong>private</strong></span><span><strong>&nbsp;</strong></span><span class="keyword"><strong>string</strong></span><span><strong>&nbsp;_UserName; &nbsp;&nbsp;</strong></span></li> <strong> </strong> <li class="alt"><span><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</strong></span><span class="keyword"><strong>private</strong></span><span><strong>&nbsp;</strong></span><span class="keyword"><strong>int</strong></span><span><strong>&nbsp;_UserAge; &nbsp;&nbsp;</strong></span></li> <strong> </strong> <li><span><strong>&nbsp;&nbsp;</strong></span></li> <strong> </strong> <li class="alt"><span><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</strong></span><span class="keyword"><strong>public</strong></span><span><strong>&nbsp;</strong></span><span class="keyword"><strong>int</strong></span><span><strong>&nbsp;UserAge &nbsp;&nbsp;</strong></span></li> <strong> </strong> <li><span><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;</strong></span></li> <strong> </strong> <li class="alt"><span><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</strong></span><span class="keyword"><strong>get</strong></span><span><strong>&nbsp;{&nbsp;</strong></span><span class="keyword"><strong>return</strong></span><span><strong>&nbsp;_UserAge;&nbsp;} &nbsp;&nbsp;</strong></span></li> <strong> </strong> <li><span><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</strong></span><span class="keyword"><strong>set</strong></span><span><strong>&nbsp;{&nbsp;_UserAge&nbsp;=&nbsp;value;&nbsp;} &nbsp;&nbsp;</strong></span></li> <strong> </strong> <li class="alt"><span><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;</strong></span></li> <strong> </strong> <li><span><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</strong></span><span class="keyword"><strong>public</strong></span><span><strong>&nbsp;</strong></span><span class="keyword"><strong>string</strong></span><span><strong>&nbsp;UserName &nbsp;&nbsp;</strong></span></li> <strong> </strong> <li class="alt"><span><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;</strong></span></li> <strong> </strong> <li><span><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</strong></span><span class="keyword"><strong>get</strong></span><span><strong>&nbsp;{&nbsp;</strong></span><span class="keyword"><strong>return</strong></span><span><strong>&nbsp;_UserName;&nbsp;} &nbsp;&nbsp;</strong></span></li> <strong> </strong> <li class="alt"><span><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</strong></span><span class="keyword"><strong>set</strong></span><span><strong>&nbsp;{&nbsp;_UserName&nbsp;=&nbsp;value;&nbsp;} &nbsp;&nbsp;</strong></span></li> <strong> </strong> <li><span><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;</strong></span></li> <strong> </strong> <li class="alt"><span><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</strong></span><span class="keyword"><strong>public</strong></span><span><strong>&nbsp;UserModel(</strong></span><span class="keyword"><strong>string</strong></span><span><strong>&nbsp;name,&nbsp;Int32&nbsp;age) &nbsp;&nbsp;</strong></span></li> <strong> </strong> <li><span><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;</strong></span></li> <strong> </strong> <li class="alt"><span><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UserName&nbsp;=&nbsp;name; &nbsp;&nbsp;</strong></span></li> <strong> </strong> <li><span><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UserAge&nbsp;=&nbsp;age; &nbsp;&nbsp;</strong></span></li> <strong> </strong> <li class="alt"><span><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;</strong></span></li> <strong> </strong> <li><span><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</strong></span><span class="keyword"><strong>public</strong></span><span><strong>&nbsp;&nbsp;</strong></span><span class="keyword"><strong>void</strong></span><span><strong>&nbsp;DisplayMember() &nbsp;&nbsp;</strong></span></li> <strong> </strong> <li class="alt"><span><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;</strong></span></li> <strong> </strong> <li><span><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(</strong></span><span class="string"><strong>"UserName:{0},UserAge:{1}"</strong></span><span><strong>,</strong></span><span class="keyword"><strong>this</strong></span><span><strong>.UserName,</strong></span><span class="keyword"><strong>this</strong></span><span><strong>.UserAge); &nbsp;&nbsp;</strong></span></li> <strong> </strong> <li class="alt"><span><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;</strong></span></li> <strong> </strong> <li><span><strong>&nbsp;&nbsp;</strong></span></li> <strong> </strong> <li class="alt"><span><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</strong></span><span class="keyword"><strong>public</strong></span><span><strong>&nbsp;</strong></span><span class="keyword"><strong>static</strong></span><span><strong>&nbsp;</strong></span><span class="keyword"><strong>void</strong></span><span><strong>&nbsp;SendUserDateByValue(UserModel&nbsp;user) &nbsp;&nbsp;</strong></span></li> <strong> </strong> <li><span><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;</strong></span></li> <strong> </strong> <li class="alt"><span><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user.UserAge&nbsp;=&nbsp;34; &nbsp;&nbsp;</strong></span></li> <strong> </strong> <li><span><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user&nbsp;=&nbsp;</strong></span><span class="keyword"><strong>new</strong></span><span><strong>&nbsp;UserModel(</strong></span><span class="string"><strong>"周宇朝"</strong></span><span><strong>,&nbsp;21); &nbsp;&nbsp;</strong></span></li> <strong> </strong> <li class="alt"><span><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;</strong></span></li> <strong> </strong> <li><span><strong>&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;</strong></span></li> <strong> </strong> <li class="alt"><span><strong>&nbsp;&nbsp;</strong></span></li> <strong> </strong> <li><span><strong>Main函数: &nbsp;&nbsp;</strong></span></li> <strong> </strong> <li class="alt"><span><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UserModel&nbsp;Model&nbsp;=&nbsp;</strong></span><span class="keyword"><strong>new</strong></span><span><strong>&nbsp;UserModel(</strong></span><span class="string"><strong>"CSDN"</strong></span><span><strong>,&nbsp;10); &nbsp;&nbsp;</strong></span></li> <strong> </strong> <li><span><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UserModel.SendUserDateByValue(Model); &nbsp;&nbsp;</strong></span></li> <strong> </strong> <li class="alt"><span><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Model.DisplayMember(); &nbsp;&nbsp;</strong></span></li> <strong> </strong> <li><span><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.ReadLine();&nbsp; <br /></strong></span></li> <li><span><br /></span></li> <li><strong>&nbsp;&nbsp;&nbsp;&nbsp; 看代码,在心中模拟,选择正确的输出结果!</strong></li> <li>&nbsp;&nbsp;&nbsp; A : UserName:周宇朝,UserAge:34</li> <li>&nbsp; &nbsp; B : UserName:CSDN,UserAge:10</li> <li>&nbsp; &nbsp; C : UserName: 周宇朝,UserName:21</li> <li>&nbsp;&nbsp;&nbsp; D : UserName:CSDN,UserAge:34</li> </ol> <p>&nbsp;</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong><span style="color: #000000;">&nbsp;&nbsp;&nbsp; 最好做出详细的解释!!!</span></strong>谢谢</p> <p>&nbsp;</p>
lixiong0802的主页 lixiong0802 | 初学一级 | 园豆:200
提问于:2010-08-14 17:03
< >
分享
最佳答案
0

选择D,

原因是: UserModel Model = new UserModel("CSDN", 10); 这个做了初始化操作,这个时候 UserName ="CSDN" UserAge=10 ,

然后      

  public static void SendUserDateByValue(UserModel user)           {   

           user.UserAge = 34;          

    user = new UserModel("周宇朝", 21);    

      }

在这个方法里面是把 Model 这个变量的值的拷贝传递进来,user.UserAge = 34;   这条语句操作的对象(mA)实际也是Model 变量引用的对象。所以现在 mA.UserAge 的值 为34,mA.UserName的值还是"CSDN" user = new UserModel("周宇朝", 21);  这条语句相当于user变量引用了一个新的对象(mB)的地址。关键是: Model 变量引用的的对象依然是原来的对象(mA),所以调用  Model.DisplayMember();   相当于输出 mA.UserName,mA.UserAge. 结果是 “CSDN”,“34”;

总结 :参数加上 ref ,out 才是 传递地址、其他的都是传递值。

对象引用变化可以分下面三个阶段:

一、UserModel Model = new UserModel("CSDN", 10);

二、UserModel.SendUserDateByValue(Model);


三、user = new UserModel("周宇朝", 21);


HUHU慈悲 | 大侠五级 |园豆:9973 | 2010-08-14 17:36
谢谢了..很详细.
lixiong0802 | 园豆:200 (初学一级) | 2010-09-01 19:27
其他回答(2)
0

千万别写出这样的代码,如果你真要在函数内 new ,请为你的函数参数指定 out 关键字;user = new UserModel("周宇朝", 21);   

很多C++的新手都犯这样的错误,还以为是封装对象的创建,结果搞的系统一堆内存泄露,虽然 .net 中有自动垃圾回收,但是还是请保持好的代码编写规范。

这个东西没啥好解释的,回去看大学课本《C语言程序设计》(谭浩强)中函数的形参和实参的区别。

Launcher | 园豆:45050 (高人七级) | 2010-08-14 17:11
0

谢谢写的这么详细,对我们新手帮助很大哈。。。

翻云~小兔子 | 园豆:223 (菜鸟二级) | 2010-08-16 15:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册