如何用接口作为参数测试静态函数?

发布于 2025-02-04 15:50:14 字数 477 浏览 3 评论 0原文

我已经看到了一个代码,但是我不知道如何设置ipoint2d startipoint2d end哪个是getangle方法的参数?

public interface IPoint2D
{
        int mX { get; set; }
        int mY { get; set; }

        void Set(int X, int Y);
        string ToString();
}
public static class GeometryAlgorithm
{
   public static double GetAngle(IPoint2D Start, IPoint2D End)
  {
        return Math.Atan((End.mY - Start.mY)/(End.mX - Start.mX)) / Math.PI * 180.0f;
    }
 }

I have seen a code, but I don't know how to set the IPoint2D Start and IPoint2D End which are the GetAngle method's parameters?

public interface IPoint2D
{
        int mX { get; set; }
        int mY { get; set; }

        void Set(int X, int Y);
        string ToString();
}
public static class GeometryAlgorithm
{
   public static double GetAngle(IPoint2D Start, IPoint2D End)
  {
        return Math.Atan((End.mY - Start.mY)/(End.mX - Start.mX)) / Math.PI * 180.0f;
    }
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静待花开 2025-02-11 15:50:14

首先,您应该定义实现此接口的类。现在是演示代码

class Point2D : IPoint2D
{
    public int mX
    {
        get;
        set;
    }

    public int mY
    {
        get;
        set;
    }

    public void Set(int X, int Y)
    {
        mX = X;
        mY = Y;
    }
}

,您可以创建此类的实例,并将其传递给getangle方法。

Point2D start = new Point2D() { mX = 10, mY = 10 };
Point2D end  = new Point2D() { mX = 20, mY = 20 };
GeometryAlgorithm.GetAngle(start, end);

First, you should define a class implementing this interface. Here is demo code

class Point2D : IPoint2D
{
    public int mX
    {
        get;
        set;
    }

    public int mY
    {
        get;
        set;
    }

    public void Set(int X, int Y)
    {
        mX = X;
        mY = Y;
    }
}

Now you can create instances of this class and pass it to GetAngle method.

Point2D start = new Point2D() { mX = 10, mY = 10 };
Point2D end  = new Point2D() { mX = 20, mY = 20 };
GeometryAlgorithm.GetAngle(start, end);
靖瑶 2025-02-11 15:50:14

接口表示对象之间的合同。如果要测试接口,则需要创建一个实现接口的对象。

如果将ipoint2d更改为point2d

    public class Point2D
    {
        int mX { get; set; }
        int mY { get; set; }

        public Point2D(int x, int y){
            mX = x;
            mY = y;
        }

    }

那么您可以这样使用它,只要您还更新静态函数以将类型point> Point2d :

    //...

    var start = Point2D(0,0);
    var end = Point2D(10,10);

    var angle = GeometryAlgorithm.GetAngle(start,end);

    //...

如果您希望将其保留为接口,则必须在类似的类中实现它:

    public class Point2D : IPoint2D
    {
        int mX { get; set; }
        int mY { get; set; }

        public Point2D(int x, int y){
            mX = x;
            mY = y;
        }

        void Set(int x, int y) {
            mX = x;
            mY = y;
        }

        public override string ToString() {
           return $"X: {mX}; Y: {mY}";
        }
    }

Interfaces represent a contract between objects. If you want to test your interface you need to create an object that implements the interface.

If you change your IPoint2D to Point2D:

    public class Point2D
    {
        int mX { get; set; }
        int mY { get; set; }

        public Point2D(int x, int y){
            mX = x;
            mY = y;
        }

    }

Then you could use it like so, as long as you also update your static function to take objects of type Point2D:

    //...

    var start = Point2D(0,0);
    var end = Point2D(10,10);

    var angle = GeometryAlgorithm.GetAngle(start,end);

    //...

If you wish to keep it as an interface, you'll have to have it implemented in a class like so:

    public class Point2D : IPoint2D
    {
        int mX { get; set; }
        int mY { get; set; }

        public Point2D(int x, int y){
            mX = x;
            mY = y;
        }

        void Set(int x, int y) {
            mX = x;
            mY = y;
        }

        public override string ToString() {
           return 
quot;X: {mX}; Y: {mY}";
        }
    }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文