0

我刚刚完成学业,我要开始申请了。在申请之前我想拥有的东西之一是一种工具箱,里面装满了我可以在任何地方使用的 dll。

我实际上正在处理一个小问题,并且真的不知道什么是“最佳实践”。

我希望能够以多种方式使用以下课程。在我的“主要”中,我希望能够以不同的方式调用某些函数:

MyDistance a = new MyDistance();
int b = a.Orthodrome(1.5 , 2.3, 5.8, 4.1);

而且,我也希望能够以这种方式使用它:

MyDistance a = new Distance(1.5 , 2.3, 5.8, 4.1);
int b = a.Orthodrome();
int c = a.Loxodrome();

我遇到的问题是,在我的“主要”中,我可以这样做:

MyDistance a = new MyDistance();
int b = a.Orthodrome();

这将始终返回错误。

他是我班级“MyDistance”的一部分,我只是提供“信息”,我不知道它是否有用。

public class MyDistances
{
    private double _Lat1;
    private double _Long1;
    private double _Lat2;
    private double _Long2;

    /// <summary>
    /// Constructeur de la classe MyDistances. Il construit la classe avec les coordonnées géographiques de 2 points.
    /// </summary>
    /// <param name="lat1">Lattitude du premier point</param>
    /// <param name="long1">Longitude du premier point</param>
    /// <param name="lat2">Lattitude du second point</param>
    /// <param name="long2">Longitude du second point</param>
    public MyDistances(double lat1, double long1, double lat2, double long2)
    {
        this._Lat1 = (lat1 * Math.PI) / 180;
        this._Lat2 = (lat2 * Math.PI) / 180;
        this._Long1 = (long1 * Math.PI) / 180;
        this._Long2 = (long2 * Math.PI) / 180;
    }

    public MyDistances()
    {
        //Do nothing here.
    }

    /// <summary>
    /// Cette méthode retourne une distance exprimée en kilomètres, entre 2 points de l'espact
    /// </summary>
    /// <param name="lat1">La lattitude du premier point, exprimée en degrés</param>
    /// <param name="long1">La longitude du premier point, exprimées en degrés</param>
    /// <param name="lat2">La lattitude du second point, exprimée en degrés</param>
    /// <param name="long2">La longitude du second point, exprimée en degrés</param>
    /// <returns>Le typ</returns>
    public int Orthodrome(double lat1, double long1, double lat2, double long2)
    {
        this._Lat1 = (lat1 * Math.PI) / 180;
        this._Lat2 = (lat2 * Math.PI) / 180;
        this._Long1 = (long1 * Math.PI) / 180;
        this._Long2 = (long2 * Math.PI) / 180;

        return Orthodrome();
    }

    public int Orthodrome()
    {
        int distance = -1;
        try
        {
            distance = (int)Math.Round(2 * 6370 * Math.Asin(Math.Sqrt((Math.Sin((_Lat1 - _Lat2) / 2) * Math.Sin((_Lat1 - _Lat2) / 2) + Math.Cos(_Lat1) * Math.Cos(_Lat2) * Math.Sin((_Long1 - _Long2) / 2) * Math.Sin((_Long1 - _Long2) / 2)))));
        }
        catch (Exception)
        {
            throw;
        }
        return distance;
    }

}

如果被调用的构造函数没有属性,我应该怎么做才能避免调用没有属性的方法?

顺便说一句,如果您发现我做错了什么,请随时发表评论。我当然还有很多东西要学。

4

3 回答 3

1

你应该做Orthodrome(double,double,double,double)静态的。

public static int Orthodrome(double lat1, double long1, double lat2, double long2)
{
    double Lat1 = (lat1 * Math.PI) / 180;
    double Lat2 = (lat2 * Math.PI) / 180;
    double Long1 = (long1 * Math.PI) / 180;
    double Long2 = (long2 * Math.PI) / 180;

    distance = CalculateOrthodrome(Lat1, Lat2, Long1, Long2);

    return distance;
}

现在在您的主要功能中,您可以执行以下操作:

MyDistance.Orthodrome(2.3, 1.5, 2.8, 3.2);

你不再需要MyDistance a = new MyDistance()提前打电话了。这类方法(函数)称为静态(非实例)。

编辑:出于您对复制代码的担忧:

private static int CalculateOrthodrome(double lat1, double long1, double lat2, double long2)
{
    return (int)Math.Round(2 * 6370 * Math.Asin(Math.Sqrt((Math.Sin((Lat1 - Lat2) / 2) * Math.Sin((Lat1 - Lat2) / 2) + Math.Cos(Lat1) * Math.Cos(Lat2) * Math.Sin((Long1 - Long2) / 2) * Math.Sin((Long1 - Long2) / 2)))));
}

现在CalculateOrthodrome对实例方法和非实例方法都使用方法。

于 2013-06-15T13:47:13.323 回答
0

只需将无参数构造函数设为私有即可:

private MyDistance() {}
于 2013-06-15T13:40:56.353 回答
0

您可以将您的字段设置为一些默认值,这样如果有人使用您的默认构造函数,您至少可以返回一些结果。

您可以在非默认构造函数中设置一个标志,并在任何无参数方法中检查它。这将允许您抛出一个明确的异常。

平心而论,我会考虑采用一种方法或另一种方法,而不是两者兼而有之。

于 2013-06-15T13:49:57.120 回答