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

推荐订阅源

爱范儿
爱范儿
Y
Y Combinator Blog
博客园 - Franky
D
Docker
B
Blog RSS Feed
M
MIT News - Artificial intelligence
雷峰网
雷峰网
博客园 - 司徒正美
人人都是产品经理
人人都是产品经理
宝玉的分享
宝玉的分享
S
SegmentFault 最新的问题
GbyAI
GbyAI
Recent Announcements
Recent Announcements
Martin Fowler
Martin Fowler
H
Hackread – Cybersecurity News, Data Breaches, AI and More
MyScale Blog
MyScale Blog
B
Blog
H
Help Net Security
Microsoft Security Blog
Microsoft Security Blog
WordPress大学
WordPress大学
Vercel News
Vercel News
The Cloudflare Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Google DeepMind News
Google DeepMind News

博客园 - 修多

mac环境下无法更新OFFICE时,可以直接下载离线升级包 AxureRP_for_chorme的安装和使用方法 解决国内gem不能用的问题 ASP操作Word的权限配置 北京天天有雨下 项目记事 声声慢-李清照 荷兰风车 要继续消失了 孤独的要窒息 消失归来 新疆帕米尔高原 长白山天池 人生的几个好习惯[转载] 今天的心情本来很好 涅槃后方能重生 为心中理想奋斗! 心态决定着今天! 从郁闷到爽,真是够爽的! 昨晚睡得较早,今早精神爽啊 联想“精神病”,文摘 3.29-4.10安排 Windows Server 2003环境下安装JBuilderX 十八个故事[转载]—值得一看 接口实现的继承机制 属性重载的简单说明 索引指示器 static readonly与使用const的区别
操作符重载
修多 · 2004-03-22 · via 博客园 - 修多

C#中,下列操作符可以重载:
+ - ! ~ ++ 00 true false
* / % & | ^ << >> == != > < >= <=
下列操作符是不允许进行重载的:
= && || ?: new typeof sizeof is

// 操作符重载演示
using System;
class Player
{
    public int neili;
    public int tili;
    public int jingyan;
    public int neili_r;
    public int tili_r;
    public Player()
    {
        neili = 10;
        tili = 50;
        jingyan = 0;
        neili_r = 50;
        tili_r = 50;
    }
   
    // 一元操作符重载
    public static Player operator ++(Player p)
    {
        p.neili = p.neili + 50;
        p.tili = p.tili + 50;
        p.neili_r = p.neili;
        p.tili_r = p.tili;
        return p;
    }
   
    // 二元操作符重载
    public static Player operator +(Player p1, Player p2)
    {
        Player p = new Player();
        p.neili = p1.neili +p2.neili;
        p.tili = p1.tili + p2.tili;
        p.neili_r = p.neili;
        p.tili_r = p.tili;
       
        return p;
    }
   
    public static void Main()
    {
        Player p1 = new Player();
        Player p2 = new Player();
        p1++;
        Console.WriteLine(" {0}", p1.neili);
       
        Player p = p1 + p2;
        Console.WriteLine(" {0}", p.neili);
    }
}