这里的运算符重载,为什么老是提示:必须包含类型??
一、题目:这道题目很有趣,运用+运算符重载,完成“苹果+水=苹果汁”二、我的努力
我信心满满地用了一个下午的时间,编写了代码。可运行时,老是提示这样的错误,
:错误 1 二元运算符的参数之一必须是包含类型
namespace 书本例题
{
class Program
{
public class AppleWater //苹果汁
{
public string name;
public string colour;
public string form;
public string taste;
}
public class Apple //苹果
{
public string name;
public string colour;
public string form;
public string taste;
public Apple(string name, string colour, string form, string taste)
{
this.name = name;
this.colour = colour;
this.form = form;
this.taste = taste;
}
public Apple()
{
this.name = "苹果";
this.colour = "红色";
this.form = "圆形";
this.taste = "酸甜";
}
}
public class Water //水
{
public string name;
public string colour;
public string form;
public string taste;
public Water(string name, string colour, string form, string taste)
{
this.name = name;
this.colour = colour;
this.form = form;
this.taste = taste;
}
public Water()
{
this.name = "水";
this.colour = "无色";
this.form = "无形";
this.taste = "无味";
}
}
public static Program operator +(Apple app, Water wat) //+运算符重载 //此处提示:错误 1 二元运算符的参数之一必须是包含类型
{
AppleWater appwat = new AppleWater();
if (app.name == "苹果" && wat.name == "水")
{
appwat.name = "苹果汁";
}
else
{
app.name = "匿名";
}
if (app.colour = "红色" || app.name = "青色" && wat.colour = "无色")
{
appwat.colour = "橙色";
}
else
{
appwat.colour = "匿色";
}
if (app.form = "圆形" && wat.form = "无形")
{
appwat.form = "无形";
}
else
{
appwat.form = "匿形";
}
if (app.taste = "酸甜" && wat.taste = "无味")
{
appwat.taste = "酸甜可口";
}
else
{
appwat.taste = "匿味";
}
return appwat;
}
public void show() //显示
{
Console.WriteLine("名称:{0}",name);
Console.WriteLine("颜色:{0}", colour);
Console.WriteLine("形状:{0}", form);
Console.WriteLine("味道:{0}", taste);
}
static void Main(string[] args)
{
Apple app = new Apple();
app.show();
Water wat = new Water();
wat.shoe();
AppleWater appwat = new AppleWater();
appwat = app + wat; //调用+运算符重载
appwat.show();
Console.ReadKey();
}
}
}
三、错误之处
在public static Program operator +(Apple app, Water wat) 此处提示:错误 1 二元运算符的参数之一必须是包含类型
小虾不知道怎么办?请高手大侠帮忙看看,哪里错了?如何纠正?小虾在此有礼了。
[ 本帖最后由 lxsxd 于 2014-8-24 20:40 编辑 ]