zoukankan      html  css  js  c++  java
  • 使用接口改变已经装箱的值类型的字段

    使用接口改变已经装箱的值类型的字段
    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace App1
    {
        
    struct Point
        
    {
            
    public Int32 x, y;
            
    public void Change(Int32 x, Int32 y)
            
    {
                
    this.x = x;
                
    this.y = y;
            }

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

        }

        
    class Program
        
    {
            
    static void Main(string[] args)
            
    {
                Point p 
    = new Point();
                p.x 
    = p.y = 1;
                Console.WriteLine(p);
                
    //调用p的ToString()方法,若不override---ToString()则输出"App1.Point"
                ///////////////////////////
                p.Change(22);
                Console.WriteLine(p);
                
    ///////////////////////////
                object o = p;//p装箱
                Console.WriteLine(o);//o指向装箱后的p---(2,2)
                //////////////////////////
                ((Point)o).Change(3, 3);//把o拆箱到临时栈上,并在临时栈上改变Point的值成(3,3)
                Console.WriteLine(o);//输出原来o指向的对象的值:(2,2)
                Console.Read();
            }

        }

    }

    ****************************************
    using System;
    using System.Collections.Generic;
    using System.Text;

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

        
    struct Point:IChangeBoxedPoint
        
    {
            
    public Int32 x, y;
            
    //现在该Change方法成了实现的接口的方法
            public void Change(Int32 x, Int32 y)
            
    {
                
    this.x = x;
                
    this.y = y;
            }

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

        }

        
    class Program
        
    {
            
    static void Main(string[] args)
            
    {
                Point p 
    = new Point();
                p.x 
    = p.y = 1;
                Console.WriteLine(p);
                
    //调用p的ToString()方法,若不override---ToString()则输出"App1.Point"
                ///////////////////////////
                p.Change(22);
                Console.WriteLine(p);
                
    ///////////////////////////
                object o = p;//p装箱
               
                Console.WriteLine(o);
    //o指向装箱后的p---(2,2)
                //////////////////////////
                ((Point)o).Change(33);//把o拆箱到临时栈上,并在临时栈上改变Point的值成(3,3)
                Console.WriteLine(o);//输出原来o指向的对象的值:(2,2)
                ///////////////////////////
                ((IChangeBoxedPoint)p).Change(4,4);//把p装箱,改变已装箱对象,随后将之丢弃
                Console.WriteLine(p);//输出p(2,2)
                //////////////////////////
                ((IChangeBoxedPoint)o).Change(5, 5);//这里o本身即是已装箱的对象,把它改变
                Console.WriteLine(o);//输出刚刚被改变的o(5,5)

                Console.Read();
            }

        }

    }

  • 相关阅读:
    FetchApi 和XHR的主要区别
    关于面试mysql优化的几点纪要
    Python学习第二天数组
    windows7__32位下安装python2.6.6
    一致性哈希算法运用到分布式
    2019年的前端面试总结
    ant design vue + ts 时遇到的坑之from 表单
    vue/cli3 + typescript 中watch prop component computed 的用法
    简易的数据追踪和并发
    基于角色的安全体系
  • 原文地址:https://www.cnblogs.com/shuang/p/1007215.html
Copyright © 2011-2022 走看看