zoukankan      html  css  js  c++  java
  • 总结: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);

            }

        }

    输出:

     

  • 相关阅读:
    关于信号的一些知识
    压缩感知综合理解篇
    稀疏编码之字典学习
    Iterator和for...of
    ES6中的Symbol
    原生node写一个静态资源服务器
    css中的流,元素,基本尺寸
    Node-Blog整套前后端学习记录
    mongodb 和 mongoose 初探
    Express 初步使用
  • 原文地址:https://www.cnblogs.com/LeimOO/p/1639096.html
Copyright © 2011-2022 走看看