要求写一个函数Add(exp1,exp2), 能运算整型, 浮点型, 字符串型及自定义类型的参数.
解决方案 »
- C#开发ktv点歌系统
- 多线程发送邮件(急) 高分请教 在线等
- 图片滑动的算法如何实现
- RationalRoseEnterprise建模是否支持c#啊?再问问建模的相关问题,先感谢了!
- 用 "select * from 图书" 这个语句查询得到的所有字段中,有几个数值字段都是4位小数,有什么办法将这些字段统一都变成2位小数
- 关于适配器模式
- 如何将dataSet11更新发回到数据源??用Update没有用。
- up者有分:将HtmlTable生成文本文件
- [使用OpenXML SDK操作Excel报表]单元格百分比的问题
- 公式求值,看似比较麻烦。给出有意义的办法就有分
- 用C#写UltraEdit32那样的编辑器,当中的关键子突出显示怎样实现
- datalist绑定button事件
//....
}
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);
}
{
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()),分别作出对应的拆箱和加法,然后结果装箱返回。
{
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);
}
}