使用接口改变已经装箱的值类型的字段
生活随笔
收集整理的這篇文章主要介紹了
使用接口改变已经装箱的值类型的字段
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用接口改變已經裝箱的值類型的字段。
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(2,?2);
????????????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(2,?2);
????????????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)
????????????/**////
????????????((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();
????????}
????}
}
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(2,?2);
????????????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(2,?2);
????????????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)
????????????/**////
????????????((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();
????????}
????}
}
轉載于:https://www.cnblogs.com/shuang/archive/2007/12/20/1007215.html
總結
以上是生活随笔為你收集整理的使用接口改变已经装箱的值类型的字段的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ibatisnet示例:npetshop
- 下一篇: operate XML file (Op