you cannot use the non-static member without the object, show the code. Here is an example for operator overloading:public static Complex operator +(Complex c1,Complex c2)
{
Complex temp = new Complex();
temp.x = c1.x+c2.x;
temp.y = c1.y+c2.y;
return temp;
}
{
Complex temp = new Complex();
temp.x = c1.x+c2.x;
temp.y = c1.y+c2.y;
return temp;
}
解决方案 »
- c# textbox的统一表示方法
- webBrowser 模拟点击
- Jmail不能发信的问题?
- C#中,如何来存储类似复选性质的属性更为合适?
- 如何判断一个字符串是否包含在一个字符数组中(字符串是数组的一个元素)?
- 怎样拖动TabControl的TabPage文本那块区域到另外一个TabPage上,即相互交换位置(WinForm)
- 如何将资源里的图片文件转换成二进制??????????????
- 我写了个FUNCTION,是用来将TABLE中的日期格式转换的,不知为什么达不到效果,请教
- 客户端响应服务端,“ADMIN”账户跟其他账户结果相差太大
- 才安的VS2003不能加Web(asp.net应用程序)
- 各位帮忙!一个奇怪的问题!
- 怎样让DataSet中的某个表按指定的字段排序?
运算符重载允许为运算指定用户定义的运算符实现,其中一个或两个操作数是用户定义的类或结构类型。本教程包含两个示例。第一个示例展示如何使用运算符重载创建定义复数加法的复数类。第二个示例展示如何使用运算符重载实现三值的逻辑类型。示例 1
本示例展示如何使用运算符重载创建定义复数加法的复数类 Complex。本程序使用 ToString 方法的重载显示数字的虚部和实部以及加法结果。// complex.cs
using System;public struct Complex
{
public int real;
public int imaginary; public Complex(int real, int imaginary)
{
this.real = real;
this.imaginary = imaginary;
} // Declare which operator to overload (+), the types
// that can be added (two Complex objects), and the
// return type (Complex):
public static Complex operator +(Complex c1, Complex c2)
{
return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
}
// Override the ToString method to display an complex number in the suitable format:
public override string ToString()
{
return(String.Format("{0} + {1}i", real, imaginary));
} public static void Main()
{
Complex num1 = new Complex(2,3);
Complex num2 = new Complex(3,4); // Add two Complex objects (num1 and num2) through the
// overloaded plus operator:
Complex sum = num1 + num2; // Print the numbers and the sum using the overriden ToString method:
Console.WriteLine("First complex number: {0}",num1);
Console.WriteLine("Second complex number: {0}",num2);
Console.WriteLine("The sum of the two numbers: {0}",sum);
}
}
输出
First complex number: 2 + 3i
Second complex number: 3 + 4i
The sum of the two numbers: 5 + 7i
示例 2
本示例展示如何使用运算符重载实现三值的逻辑类型。该类型的可能值有 DBBool.dbTrue、DBBool.dbFalse 和 DBBool.dbNull,其中 dbNull 成员表示未知值。注意 定义 True 和 False 运算符只对表示 True、False 和 Null(空)(既非 True 也非 False)的类型有用,如数据库中使用的类型。
// dbbool.cs
using System;public struct DBBool
{
// The three possible DBBool values:
public static readonly DBBool dbNull = new DBBool(0);
public static readonly DBBool dbFalse = new DBBool(-1);
public static readonly DBBool dbTrue = new DBBool(1);
// Private field that stores -1, 0, 1 for dbFalse, dbNull, dbTrue:
int value; // Private constructor. The value parameter must be -1, 0, or 1:
DBBool(int value)
{
this.value = value;
} // Implicit conversion from bool to DBBool. Maps true to
// DBBool.dbTrue and false to DBBool.dbFalse:
public static implicit operator DBBool(bool x)
{
return x? dbTrue: dbFalse;
} // Explicit conversion from DBBool to bool. Throws an
// exception if the given DBBool is dbNull, otherwise returns
// true or false:
public static explicit operator bool(DBBool x)
{
if (x.value == 0) throw new InvalidOperationException();
return x.value > 0;
} // Equality operator. Returns dbNull if either operand is dbNull,
// otherwise returns dbTrue or dbFalse:
public static DBBool operator ==(DBBool x, DBBool y)
{
if (x.value == 0 || y.value == 0) return dbNull;
return x.value == y.value? dbTrue: dbFalse;
} // Inequality operator. Returns dbNull if either operand is
// dbNull, otherwise returns dbTrue or dbFalse:
public static DBBool operator !=(DBBool x, DBBool y)
{
if (x.value == 0 || y.value == 0) return dbNull;
return x.value != y.value? dbTrue: dbFalse;
} // Logical negation operator. Returns dbTrue if the operand is
// dbFalse, dbNull if the operand is dbNull, or dbFalse if the
// operand is dbTrue:
public static DBBool operator !(DBBool x)
{
return new DBBool(-x.value);
} // Logical AND operator. Returns dbFalse if either operand is
// dbFalse, dbNull if either operand is dbNull, otherwise dbTrue:
public static DBBool operator &(DBBool x, DBBool y)
{
return new DBBool(x.value < y.value? x.value: y.value);
} // Logical OR operator. Returns dbTrue if either operand is
// dbTrue, dbNull if either operand is dbNull, otherwise dbFalse:
public static DBBool operator |(DBBool x, DBBool y)
{
return new DBBool(x.value > y.value? x.value: y.value);
} // Definitely true operator. Returns true if the operand is
// dbTrue, false otherwise:
public static bool operator true(DBBool x)
{
return x.value > 0;
} // Definitely false operator. Returns true if the operand is
// dbFalse, false otherwise:
public static bool operator false(DBBool x)
{
return x.value < 0;
} // Overload the conversion from DBBool to string:
public static implicit operator string(DBBool x)
{
return x.value > 0 ? "dbTrue"
: x.value < 0 ? "dbFalse"
: "dbNull";
} // Override the Object.Equals(object o) method:
public override bool Equals(object o)
{
try
{
return (bool) (this == (DBBool) o);
}
catch
{
return false;
}
} // Override the Object.GetHashCode() method:
public override int GetHashCode()
{
return value;
} // Override the ToString method to convert DBBool to a string:
public override string ToString()
{
switch (value)
{
case -1:
return "DBBool.False";
case 0:
return "DBBool.Null";
case 1:
return "DBBool.True";
default:
throw new InvalidOperationException();
}
}
}class Test
{
static void Main()
{
DBBool a, b;
a = DBBool.dbTrue;
b = DBBool.dbNull; Console.WriteLine( "!{0} = {1}", a, !a);
Console.WriteLine( "!{0} = {1}", b, !b);
Console.WriteLine( "{0} & {1} = {2}", a, b, a & b);
Console.WriteLine( "{0} | {1} = {2}", a, b, a | b);
// Invoke the true operator to determine the Boolean
// value of the DBBool variable:
if (b)
Console.WriteLine("b is definitely true");
else
Console.WriteLine("b is not definitely true");
}
}
输出
!DBBool.True = DBBool.False
!DBBool.Null = DBBool.Null
DBBool.True & DBBool.Null = DBBool.Null
DBBool.True | DBBool.Null = DBBool.True
b is not definitely true
static函数自然不能访问“当前对象的”nonstatic成员,因为根本没有当前对象this实例,operator overloading也不例外。
你在operator函数里访问的是从参数里传进来的对象,又不是this,这种做法在任何static函数里都没有问题。
你的 教程 是原创的 还是 电子图书
我刚看的时候,都不知道楼顶在说什么。根本就没有使用非静态字段嘛...