要求写一个函数Add(exp1,exp2), 能运算整型, 浮点型, 字符串型及自定义类型的参数.

解决方案 »

  1.   

    public static object Add(object exp1,object exp2){
    //....
    }
      

  2.   


    private int Add(object exp1,object exp2)
    {
       //代码
    }public int Add(int exp1,int exp2)
    {
       return Add((object)exp1,(object)exp2);
    }public int Add(double exp1,double exp2)
    {
       return Add((object)exp1,(object)exp2);
    }public int Add(string exp1,string exp2)
    {
       return Add((object)exp1,(object)exp2);
    }
      

  3.   

    using System;class A
    {
        public int i;
        public string s;
        // 无参构造器
        public A() 
            : this(0, null) { }
        // 有参构造器
        public A(int i, string s)
        {
            this.i = i;
            this.s = s;
        }
        // 自定义类重载操作符 '+'
        // 把类里的参数相加
        public static A operator + (A lhs ,A rhs)
        {
            A result=new A();
            result.i= lhs.i + rhs.i;
            result.s = lhs.s + rhs.i;
            return result;
        }
    }
    class Operator
    {
        // 4 种类型的重载方法,参数和返回值类型也可以自己随意定
        // 重载方法也可以是动态的,这里图省事全用 static 静态的了
        public static int Add(int lhs, int rhs)
        { return lhs + rhs; }
        
        public static float Add(float lhs, float rhs)
        { return lhs + rhs; }
        
        public static string Add(string lhs, string rhs)
        { return lhs + rhs; }    public static A Add(A lhs, A rhs)
        // 下面这句用到了自己定义的重载操作符,如果找不到重载操作符就没法编译
        { return lhs + rhs; }
    }public class Program
    {    static void Main(string[] args)
        {
            int ia = 1;
            int ib = 2;
            int ic = Operator.Add(ia, ib);
            Console.WriteLine(ac);        float fa = 1f;
            float fb = 2f;
            float ic = Operator.Add(fa, fb);
            Console.WriteLine(ac);        string sa = "1";
            string sb = "2";
            string sc = Operator.Add(sa, sb);
            Console.WriteLine(ac);        A aa = new A(1, "1");
            A ab = new A(2, "2");
            A ac = Operator.Add(aa, ab);
            Console.WriteLine(ac);
        }
    }虽然泛型也可以做,但更麻烦,还要为每一种类型设计方法加入泛型委托。
    参数类型事先不能确定的话,也可以设定为object类型,然后在Add()方法中加入switch(obj.GetType()),分别作出对应的拆箱和加法,然后结果装箱返回。
      

  4.   

    using System;class A
    {
        public int i;
        public string s;
        // 无参构造器
        public A() 
            : this(0, null) { }
        // 有参构造器
        public A(int i, string s)
        {
            this.i = i;
            this.s = s;
        }
        // 自定义类重载操作符 '+'
        // 把类里的参数相加
        public static A operator + (A lhs ,A rhs)
        {
            A result=new A();
            result.i= lhs.i + rhs.i;
            result.s = lhs.s + rhs.i;
            return result;
        }
    }
    class Operator
    {
        // 4 种类型的重载方法,参数和返回值类型也可以自己随意定
        // 重载方法也可以是动态的,这里图省事全用 static 静态的了
        public static int Add(int lhs, int rhs)
        { return lhs + rhs; }
        
        public static float Add(float lhs, float rhs)
        { return lhs + rhs; }
        
        public static string Add(string lhs, string rhs)
        { return lhs + rhs; }    public static A Add(A lhs, A rhs)
        // 下面这句用到了自己定义的重载操作符,如果找不到重载操作符就没法编译
        { return lhs + rhs; }
    }public class Program
    {    static void Main(string[] args)
        {
            int ia = 1;
            int ib = 2;
            int ic = Operator.Add(ia, ib);
            Console.WriteLine(ic);        float fa = 1f;
            float fb = 2f;
            float fc = Operator.Add(fa, fb);
            Console.WriteLine(fc);        string sa = "1";
            string sb = "2";
            string sc = Operator.Add(sa, sb);
            Console.WriteLine(sc);        A aa = new A(1, "1");
            A ab = new A(2, "2");
            A ac = Operator.Add(aa, ab);
            Console.WriteLine(ac);
        }
    }