简体   繁体   English

在已调用的构造函数的功能中禁用一些方法

[英]Disabling some methods in function of the constructor that has been called

I've just finished my studies and I'm going to begin to apply. 我刚完成学业,即将开始申请。 One of the things I'd like to have before applying is a kind of toolbox, full of dlls that I'll be able to use everywhere. 在应用之前,我想拥有的一件事是一种工具箱,里面充满了可以在任何地方使用的dll。

I'm actually dealing with a little problem, and don't really know what would be "best practice" for that. 我实际上正在处理一个小问题,并且真的不知道什么是“最佳实践”。

I want the following class to be able to be used in plural ways. 我希望下面的类能够以多种方式使用。 In my "Main", I want to be able to call some function in different ways : 在我的“ Main”中,我希望能够以不同的方式调用某些函数:

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

And, I also want to be able to use it that way : 而且,我也希望能够以这种方式使用它:

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

The problem I have, is that in my "Main", I'm allowed to do this : 我的问题是,在我的“主要”中,我可以这样做:

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

Which will always return an error. 这将始终返回错误。

He is a part of my class "MyDistance", I just give "for information", I don't know if it might be useful or not. 他是我的课程“ 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;
    }

}

What should I do to avoid being able to call the method without attributes, if the constructor that was called did not have any? 如果被调用的构造函数没有属性,应该怎么做才能避免在没有属性的情况下调用该方法?

By the way, if you find anything else I'm doing wrong, do not hesitate to comment. 顺便说一句,如果您发现我在做任何其他事情,请立即发表评论。 I certainly still have a lot to learn. 我当然还有很多东西要学。

You should make Orthodrome(double,double,double,double) static. 您应该使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;
}

Now in your main, you can do the following: 现在,您可以执行以下操作:

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

You don't need to call MyDistance a = new MyDistance() in advance anymore. 您不再需要预先将MyDistance a = new MyDistance()调用。 These kind of methods (functions) are called static (non-instance). 这些方法(函数)称为静态(非实例)。

Edit: For your concern of copying code around: 编辑:对于您的代码复制周围的关注:

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)))));
}

Now use CalculateOrthodrome method for both instance and non instance methods. 现在将CalculateOrthodrome方法用于实例方法和非实例方法。

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

private MyDistance() {}

You could set your fields to some default values so you can return at least some result if someone used your default constructor. 您可以将字段设置为一些默认值,以便在有人使用默认构造函数的情况下至少返回一些结果。

You could set a flag in your non-default constructor and check it in any parameterless methods. 您可以在非默认构造函数中设置标志,然后在任何无参数方法中进行检查。 This would allow you to throw a clear exception. 这将使您抛出明确的异常。

In all fairness I would consider doing one method or the other, not both. 公平地说,我会考虑采用一种方法或另一种方法,而不是两种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM