码迷,mamicode.com
首页 > 其他好文 > 详细

装箱、拆箱练习

时间:2015-05-08 17:57:49      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:

【示例代码】

 1 using System;
 2 //Point is a Value Type
 3 internal struct Point {
 4     private Int32 m_x, m_y;
 5 
 6     public Point(Int32 x,Int32 y){
 7         m_x = x;
 8         m_y = y;
 9     }
10 
11     public void Change(Int32 x, Int32 y){
12         m_x = x;
13         m_y = y;
14     }
15 
16     public override String ToString(){
17         return String.Format("({0}, {1})",m_x, m_y);
18     }
19 
20     public sealed class Program {
21         public static void Main(){
22             Point p = new Point(1, 1);
23 
24             Console.WriteLine(p); //1,1 有装箱操作Console.WriteLine(Object)
25 
26             p.Change(2, 2);
27             Console.WriteLine(p); //2,2 有装箱操作Console.WriteLine(Object)
28 
29             Object o = p; //有装箱操作
30             Console.WriteLine(o); //2,2 无装箱
31              
32              ((Point) o).Change(3, 3); //拆箱并将Point对象复制到栈上临时变量中,并执行Change方法
33              Console.WriteLine(o); //2,2
34         }
35     }
36 }

装箱、拆箱练习

标签:

原文地址:http://www.cnblogs.com/lishidefengchen/p/4488302.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!