日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用接口改变已经装箱的值类型的字段

發布時間:2025/7/25 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用接口改变已经装箱的值类型的字段 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用接口改變已經裝箱的值類型的字段
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

總結

以上是生活随笔為你收集整理的使用接口改变已经装箱的值类型的字段的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。