惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

腾讯CDC
Schneier on Security
Schneier on Security
B
Blog RSS Feed
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
A
About on SuperTechFans
Recorded Future
Recorded Future
Recent Announcements
Recent Announcements
Microsoft Security Blog
Microsoft Security Blog
L
LangChain Blog
Hugging Face - Blog
Hugging Face - Blog
The GitHub Blog
The GitHub Blog
Google DeepMind News
Google DeepMind News
T
Tailwind CSS Blog
Vercel News
Vercel News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
MyScale Blog
MyScale Blog
V2EX - 技术
V2EX - 技术
N
Netflix TechBlog - Medium
F
Fortinet All Blogs
V
Visual Studio Blog
Martin Fowler
Martin Fowler
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - Franky
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
The Exploit Database - CXSecurity.com
F
Full Disclosure
Scott Helme
Scott Helme
H
Heimdal Security Blog
博客园 - 叶小钗
Google DeepMind News
Google DeepMind News
Cyberwarzone
Cyberwarzone
Application and Cybersecurity Blog
Application and Cybersecurity Blog
V
Vulnerabilities – Threatpost
Blog — PlanetScale
Blog — PlanetScale
Security Latest
Security Latest
WordPress大学
WordPress大学
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Troy Hunt's Blog
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
Jina AI
Jina AI
S
Securelist
小众软件
小众软件
Simon Willison's Weblog
Simon Willison's Weblog
J
Java Code Geeks
AWS News Blog
AWS News Blog
N
News and Events Feed by Topic
博客园 - 三生石上(FineUI控件)
量子位

博客园 - shuang

heart or house? Dos for -- by 随风 - shuang C--Pointer Raid -----asp web 报表 - shuang ----[创业指南]给海归技术创业兄弟的九个忠告 --设计模式--Hibernate about avast job desc 把INT转换成2进制等 private/实现有多个相同方法的接口 struct也有方法成员 virtual 集成资源文件 const -readonly- static readonly 脚本---用函数模拟类 csm2 ClientScriptManager html______1
使用接口改变已经装箱的值类型的字段
shuang · 2007-12-20 · via 博客园 - shuang

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

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();
        }

    }

}