日期:2014-05-18  浏览次数:20824 次

重载为什么错了 exmeple c = number1 + b;不是重载了吗
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication7
{
  class Program
  {
  static void Main(string[] args)
  {
  int number1, number2;
  Console.WriteLine("input a number ");
  number1 = int.Parse(Console.ReadLine());
  Console.WriteLine("input a number");
  number2 = int.Parse(Console.ReadLine());
   
  exmeple b = new exmeple(number2);
  // exmeple c = new exmeple(300);
  exmeple c = number1 + b;
  //Console.WriteLine("{0}", a.Number);
  // Console.WriteLine("{0}", b.Number);
  Console.WriteLine("{0}", c.Number);
  Console.Read();
  }
  }
  public class exmeple
  {
  private int number;
   

  public exmeple(int p)
  {
  this.number =p;
  }

  public int Number
  {
  get { return number; }
  set { number = value; }
  }
  public static exmeple operator +(exmeple a, int b)
  {
  return new exmeple(b) + a;
  }
  public static exmeple operator +(exmeple a, exmeple b)
  {
  return new exmeple(a.number*b.number);
  }
   

  }
}


------解决方案--------------------
exmeple c = number1 + b;
这个方法没有重载吧,换个顺序
exmeple c = b+number1;
这样就可以了,或者再加个重载办法
------解决方案--------------------
追加重载
public static exmeple operator +(int a, exmeple b)
{
return new exmeple(a) + b;
}