| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 568 人关注过本帖
标题:关于++ 运算符重载的问题
只看楼主 加入收藏
QQ_ForLove
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-5-12
收藏
 问题点数:0 回复次数:0 
关于++ 运算符重载的问题
using System;
using System.Collections.Generic;
using System.Text;

namespace Test11_38
{
    class Distance
    {
        private double Meter = 0;
        public Distance(double M)
        {
            this.Meter = M;
        }

        public double getmeter{
        get
        {
            return Meter;
        }
            set
            {
                Meter =value;
            }
        }

        public static Distance operator + (Distance M1, Distance M2)
        {
            return new Distance(M1.Meter + M2.Meter);
        }

        public static Distance operator * (Distance M1, Distance M2)
        {
            return new Distance(M1.Meter * M2.Meter);
        }

      /**  public static Distance operator ++(Distance M1)
        {
            return new Distance(M1.Meter++);
        }
       **/

        public static Distance operator ++(Distance M)
        {
            M.getmeter++;
            return M;
        }
      
    }
   

    class Surface
    {
        private double Cube;
        public Surface(double C)
        {
            this.Cube = C;
        }

        public double GetCube
        {
            get
            {
                return Cube;
            }
            set
            {
                Cube = value;
            }
        }

        public static Surface operator + (Surface C1, Surface C2)
        {
            return new Surface(C1.Cube + C2.Cube);
        }

        public static Surface operator * (Surface C1, Distance M1)
        {
            return new Surface(C1.Cube * M1.getmeter);
        }

        public static Surface operator *(Distance M1, Surface C1)
        {
            return C1 * M1;
        }
      
    }
        
         
    class Program
    {
        static void Main(string[] args)
        {
            Distance M1, M2;
            M1 = new Distance(10.0);
            M2 = new Distance(70.0);
            Surface C1;
            C1 = new Surface(8.0);
            Console.Write("The result is " + (M1.getmeter + M2.getmeter));
            Console.Write("\n");
            Console.Write("Another result is" + C1.GetCube * M1.getmeter);
            Console.Write("\n");   
            Distance M3 = M1++;
            M2 = ++M3;
            Console.Write("after ++ The result is {0},{1},{2}",M1.getmeter,M2.getmeter,M3.getmeter);
        }
    }
}

结果为12 12 12  为什么重载以后不能得到是11 13 13呢 或者其它结果 希望大家解释下具体重载调用过程
搜索更多相关主题的帖子: 运算符 重载 
2008-06-24 15:40
快速回复:关于++ 运算符重载的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020594 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved