在不需要由所述类的对象调用的类中创建方法

时间:2013-06-13 22:57:30

标签: c# class methods

我不太确定如何很好地描述这个问题,如果这很难理解,请道歉:

就像练习一样(我还是C#的新手)我想创建一个类,Point,就像坐标网格上的点一样。到目前为止我有这个:

using System;
using System.Collections.Generic;
using System.Text;

namespace Point_Class
{
    class Point
    {
        private int x, y;
        public Point() 
        {
            Console.WriteLine("Default Constructor Loaded.");
            this.x = 0;
            this.y = 0;
        }
        public Point(int x, int y)
        {
            this.x = x;
            this.y = y;
        }

        public string Equation(Point p1, Point p2)
        {

        }
    }

    class Program 
    {
        static void Main(string[] args)
        {
            Point x,y;
            x = new Point(2, 2);
            y = new Point(5, 6);
            x.DistanceTo(y);
            Console.ReadLine();
        }
    }
}

现在,我的问题是:有没有办法像这样运行方程式(函数或方法,不确定术语)

Equation(Point x, Point y);

还是必须与众不同? 感谢。

1 个答案:

答案 0 :(得分:3)

让它静止:

class Point
{
    public static string Equation(Point p1, Point p2)
    {
       ...
    }
}

现在你可以用

来调用它
var result = Point.Equation(x, y);