You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C# Point类中实现distance方法计算两点间距离?

问题解决:实现Point类的distance方法并调用

1. 先理解Point other参数的含义

当你在一个Point对象(比如a)上调用distance方法时,other就是你传入的另一个Point对象(比如b)。方法内部:

  • 当前对象的坐标可以直接用类里的_x、_y(或者通过getXCoordinate()、getYCoordinate()获取)
  • other对象的坐标则通过other._x、other._y(或对应的getter方法)获取

2. 实现distance方法

在Point类中添加以下方法,按照公式计算两点距离:

public double distance(Point other)
{
    // 计算两个点的x、y坐标差
    double xDifference = this._x - other._x;
    double yDifference = this._y - other._y;
    // 计算平方和
    double squaredSum = xDifference * xDifference + yDifference * yDifference;
    // 开根号得到距离,坐标相同时squaredSum为0,返回0.0
    return Math.Sqrt(squaredSum);
}

如果你习惯用getter方法(而非直接访问成员变量),也可以写成:

public double distance(Point other)
{
    double xDifference = this.getXCoordinate() - other.getXCoordinate();
    double yDifference = this.getYCoordinate() - other.getYCoordinate();
    double squaredSum = xDifference * xDifference + yDifference * yDifference;
    return Math.Sqrt(squaredSum);
}

3. 在Main方法中调用distance方法

找到Main方法里的这一行:

Console.WriteLine("Distance from A to B: ");

修改为调用a.distance(b)并输出结果:

Console.WriteLine("Distance from A to B: " + a.distance(b));

你也可以调用b.distance(a),因为两点距离是双向的,结果完全一致。

修改后的完整代码

//Dimitar Kapitanov 11/6
using System;
public class PointClassPt2
{
    public static void Main(String[] args)
    {
        //main method
        Point a = new Point();

        Console.WriteLine("First Point");
        Console.Write("X: ");
        double x = Convert.ToDouble(Console.ReadLine());
        Console.Write("Y: ");
        double y = Convert.ToDouble(Console.ReadLine());
        a.setCoordinate(x, y);

        Point b = new Point(x, y);

        Console.WriteLine("\nSecond Point");
        Console.Write("X: ");
        x = Convert.ToDouble(Console.ReadLine());
        Console.Write("Y: ");
        y = Convert.ToDouble(Console.ReadLine());
        b.setCoordinate(x, y);


        Console.WriteLine("\nPoint A: (" + a.getXCoordinate() + " , " + a.getYCoordinate() + ")");

        Console.WriteLine("Point B: (" + b.getXCoordinate() + " , " + b.getYCoordinate() + ")");

        Point c = new Point();
        c.setCoordinate(-x, -y);

        Console.WriteLine("Point C: (" + c.getXCoordinate() + " , " + c.getYCoordinate() + ")");

        // 调用distance方法并输出结果
        Console.WriteLine("Distance from A to B: " + a.distance(b));

    }

}

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 double getXCoordinate()
    {
        return _x;
    }

    public double getYCoordinate()
    {
        return _y;
    }

    public void setCoordinate(double x, double y)
    {
        _x = x;
        _y = y;
    }

    // 添加的distance方法
    public double distance(Point other)
    {
        double xDifference = this._x - other._x;
        double yDifference = this._y - other._y;
        double squaredSum = xDifference * xDifference + yDifference * yDifference;
        return Math.Sqrt(squaredSum);
    }

}

内容的提问来源于stack exchange,提问作者Dimitar Kapitanov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 16:50:25