构建一个类Point,它提供两个公有的构造函数,一个没有参数的Point构造函数和一个有两个double参数的构造函数。另外在该类中提供一个静态方法计算两个点的直线距离,传入参数为两个Point类实例。然后设计一个测试类来对Point类进行使用。
提示:先定义两个变量来存储Point点的X,Y坐标;无参的构造函数将X,Y坐标赋为0;有参的构造函数将传入的参数分别赋给X,Y坐标。以下是我的代码:
using System;
using System.Collections.Generic;
using System.Text;namespace Point
{
class Point
{
public double X;
public double Y;
public Point()
{
X = 0;
Y = 0;
}
public Point(double x, double y)
{
X = x;
Y = y;
}
public static double distance(Point X, Point Y)
{
return (X.X - Y.X) * (X.X - Y.X) - (X.Y - X.Y) * (X.Y - X.Y);
}
public static void Main(string[] args)
{
double x, y;
x = double.Parse(Console.ReadLine());
y = double.Parse(Console.ReadLine());
Point Point = new Point(x,y);
double m = Point.distance(Point.X,Point.Y);
Console.WriteLine(m);
}
}
}
问题:题目中的“另外在该类中提供一个静态方法计算两个点的直线距离,传入参数为两个Point类实例。然后设计一个测试类来对Point类进行使用。”思路模糊,而且代码中有错误,该如何修改?请指点,谢谢~~
提示:先定义两个变量来存储Point点的X,Y坐标;无参的构造函数将X,Y坐标赋为0;有参的构造函数将传入的参数分别赋给X,Y坐标。以下是我的代码:
using System;
using System.Collections.Generic;
using System.Text;namespace Point
{
class Point
{
public double X;
public double Y;
public Point()
{
X = 0;
Y = 0;
}
public Point(double x, double y)
{
X = x;
Y = y;
}
public static double distance(Point X, Point Y)
{
return (X.X - Y.X) * (X.X - Y.X) - (X.Y - X.Y) * (X.Y - X.Y);
}
public static void Main(string[] args)
{
double x, y;
x = double.Parse(Console.ReadLine());
y = double.Parse(Console.ReadLine());
Point Point = new Point(x,y);
double m = Point.distance(Point.X,Point.Y);
Console.WriteLine(m);
}
}
}
问题:题目中的“另外在该类中提供一个静态方法计算两个点的直线距离,传入参数为两个Point类实例。然后设计一个测试类来对Point类进行使用。”思路模糊,而且代码中有错误,该如何修改?请指点,谢谢~~
using System;
using System.Collections.Generic;
using System.Text; namespace PointNameSpace
{
public class Point
{
public double X;
public double Y;
public Point()
{
X = 0.0;
Y = 0.0;
}
public Point(double x, double y)
{
X = x;
Y = y;
}
public static double distance(Point A, Point B)
{
return (A.X - B.X) * (A.X - B.X) - (A.Y - B.Y) * (A.Y - B.Y);
}
public static void Main(string[] args)
{
double x1, y;
x1 = double.Parse(Console.ReadLine());
y1 = double.Parse(Console.ReadLine());
Point point1 = new Point(x1,y1);
double x2, y2;
x2 = double.Parse(Console.ReadLine());
y2 = double.Parse(Console.ReadLine());
Point point2 = new Point(x2,y2);
double m = Point.distance(point1,point2);
Console.WriteLine(m);
}
}
}
在建立一个测试项目,在测试方法下,调用你的代码即可.
{
return Math.Sqrt((A.X - B.X) * (A.X - B.X) + (A.Y - B.Y) * (A.Y - B.Y));
}