如何:测试 Point4D 结构是否相等和不相等
更新:2007 年 11 月
本示例演示如何测试 Point4D 结构是相等还是不相等。
下面的代码阐释如何使用 Point4D 相等方法来测试 Point4D 结构是相等还是不相等。 先使用重载的 == 运算符来测试 Point4D 是否相等,然后使用重载的 != 运算符来测试这些结构是否不相等,最后使用静态 Equals 方法来检查 Point3D 结构和 Point4D 结构是否相等。
示例
// instantiate Points
Point4D point4D1 = new Point4D();
Point4D point4D2 = new Point4D(15, 40, 60, 75);
Point3D point3D1 = new Point3D(15, 40, 60);
// result variables
Boolean areEqual;
Boolean areNotEqual;
String stringResult;
// defining x,y,z,w of point1
point4D1.X = 10;
point4D1.Y = 5;
point4D1.Z = 1;
point4D1.W = 4;
// checking if Points are equal
areEqual = point4D1 == point4D2;
// areEqual is False
// checking if Points are not equal
areNotEqual = point4D1 != point4D2;
// areNotEqual is True
if (Point4D.Equals(point4D1, point3D1))
{
// the if condition is not true, so this block will not execute
stringResult = "Both objects are Point4D structures and they are equal";
}
else
{
// the if condition is false, so this branch will execute
stringResult = "Parameters are not both Point4D strucutres, or they are but are not equal";
}