[读书]看看你有没有忽视

今天利用一点休息时间,随便翻了翻桌子上摆的一大摞书,看到了一小段代码,你也试试。
using System;
using System.Collections.Generic;
using System.Text;

using System.IO;

namespace DotNetFramework
{
    
class Program
    {
        
static void Main(string[] args)
        {
            Point point 
= new Point();

            point.x 
= point.y = 1;
            Console.WriteLine(point);

            point.Change(
22);
            Console.WriteLine(point);

            
object o = point;
            Console.WriteLine(o);

            ((Point)o).Change(
33);
            Console.WriteLine(o);

            ((IChangeBoxedPoint)point).Change(
44);
            Console.WriteLine(point);

            ((IChangeBoxedPoint)o).Change(
55);
            Console.WriteLine(o);

            Console.Read();
        }

        
interface IChangeBoxedPoint
        {
            
void Change(Int32 x, Int32 y);
        }

        
struct Point : IChangeBoxedPoint
        {
            
public Int32 x;
            
public Int32 y;

            
public void Change(int x, int y)
            {
                
this.x = x;
                
this.y = y;
            }

            
public override string ToString()
            {
                
return string.Format("({0},{1})", x, y);
            }
        }
    }
}
写出程序运行结果。
using System;
using System.Collections.Generic;
using System.Text;

using System.IO;

namespace DotNetFramework
{
    
class Program
    {
        
static void Main(string[] args)
        {
            Point point 
= new Point();

            point.x 
= point.y = 1;
            
// 显示(1,1)
            Console.WriteLine(point);

            point.Change(
22);
            
// 显示(2,2)
            Console.WriteLine(point);

            
object o = point;
            
// 显示(2,2)
            Console.WriteLine(o);

            
// 在堆栈上改变临时的point
            ((Point)o).Change(33);
            
// 显示(2,2)
            Console.WriteLine(o);

            
// 对point执行装箱,然后改变以装箱对象,最后将之丢弃
            ((IChangeBoxedPoint)point).Change(44);
            
// 显示(2,2)
            Console.WriteLine(point);

            
// 改变以装箱对象,并显示其内容
            ((IChangeBoxedPoint)o).Change(55);
            
// 显示(5,5)
            Console.WriteLine(o);

            Console.Read();
        }

        
interface IChangeBoxedPoint
        {
            
void Change(Int32 x, Int32 y);
        }

        
struct Point : IChangeBoxedPoint
        {
            
public Int32 x;
            
public Int32 y;

            
public void Change(int x, int y)
            {
                
this.x = x;
                
this.y = y;
            }

            
public override string ToString()
            {
                
return string.Format("({0},{1})", x, y);
            }
        }
    }
}

posted on 2006-06-08 14:46  萝卜青菜  阅读(325)  评论(0编辑  收藏  举报

导航