• 总结:CLR Via C#(第五章):值类型与引用类型——装箱和拆箱


    struct

     class Program

        {

            static void Main(string[] args)

            {

                Point p = new Point(1, 1);

                Console.WriteLine(p);

     

                p.Change(2, 2);

                Console.WriteLine(p);

     

                object o = p;

                Console.WriteLine(o);

     

                ((Point)o).Change(3, 3);

                Console.WriteLine(o);

     

                p = (Point)o;

                p.Change(3, 3);

                Console.WriteLine(p);

            }

        }

     

        internal struct Point

        {

            Int32 m_x, m_y;

     

            public Point(Int32 x, Int32 y)

            {

                m_x = x;

                m_y = y;

            }

     

            public void Change(Int32 x, Int32 y)

            {

                m_x = x;

                m_y = y;

            }

     

            public override string ToString()

            {

                return string.Format("({0},{1})", m_x, m_y);

            }

        }

             输出:

     

    Class

    如果将Point改为Class类型,则输出:


     

    Interface

        class Program

        {

            static void Main(string[] args)

            {

                Point p = new Point(1, 1);

                Console.WriteLine(p);

     

                p.Change(2, 2);

                Console.WriteLine(p);

     

                object o = p;

                Console.WriteLine(o);

     

                ((Point)o).Change(3, 3);

                Console.WriteLine(o);

     

                p = (Point)o;

                p.Change(3, 3);

                Console.WriteLine(p);

     

     

                ((IChangeBoxedPoint)p).Change(4, 4);

                Console.WriteLine(p);

     

                ((IChangeBoxedPoint)o).Change(5, 5);

                Console.WriteLine(o);

     

                Console.ReadLine();

            }

        }

     

        internal interface IChangeBoxedPoint

        {

            void Change(Int32 x, Int32 y);

        }

     

        internal struct Point:IChangeBoxedPoint

        {

            Int32 m_x, m_y;

     

            public Point(Int32 x, Int32 y)

            {

                m_x = x;

                m_y = y;

            }

     

            public void Change(Int32 x, Int32 y)

            {

                m_x = x;

                m_y = y;

            }

     

            public override string ToString()

            {

                return string.Format("({0},{1})", m_x, m_y);

            }

        }

    输出:

     

  • 相关阅读:
    POJ 1789
    南华大学 复读机(并查集)
    C
    F
    POJ 1988 Cube Stacking
    并查集(一)
    把采集到的数据发送到一个Google Docs或者Google Form上 这个网站提供了参考和例子
    几种空气颗粒物和空气质量传感器
    整流桥
    STM32 中的CEC
  • 原文地址:https://www.cnblogs.com/LeimOO/p/1639096.html
Copyright © 2020-2023  润新知