简体   繁体   English

C#如何在不使方法静态的情况下从界面内的另一个类调用方法?

[英]C# How do I call a method from another class inside of my interface without making the method static?

I am trying to ping google to check to see if I am connected to the internet, and I want to implement this in one of my interfaces, but I am unsure how to do this as my method is not static. 我试图ping谷歌检查我是否连接到互联网,我想在我的一个接口中实现这一点,但我不确定如何做到这一点,因为我的方法不是静态的。 Any help or insight would be much appreciated. 任何帮助或见解将不胜感激。 The concepts of interfaces are new to me, so any resources that I could use to understand them better would also be very helpful! 接口的概念对我来说是新的,所以我可以用来更好地理解它们的任何资源也会非常有用! checkConnection() was where I was looking to start, maybe creating a constructor to instantiate the method? checkConnection()是我想要开始的地方,也许是创建一个构造函数来实例化该方法? but I really am unsure how to implement this. 但我真的不确定如何实现这一点。 This is what I have so far: 这是我到目前为止:

public class ApplicationManager : IApplicationManager
{

    public void ManagerRun()
    {

    if (InternetConnectionCheck.CheckForInternetConnection() == true)
    {

    }
    else if(InternetConnectionCheck.CheckForInternetConnection() == false)
    {

    }

public interface IApplicationManager
{
    void ManagerRun();
    bool checkConnection();
}

}

I cut out most of my code as it isn't relevant to the problem, but the issue is that "InternetConnectionCheck.CheckForInternetConnection() used to be a static method but it is no longer a static method, as I want to implement it in the interface so that I can call it once, and then just reference it throughout the rest of my code, whenever I want to make an internet connection check. This is the code for the check: 我删除了大部分代码,因为它与问题无关,但问题是“InternetConnectionCheck.CheckForInternetConnection()曾经是一个静态方法,但它不再是静态方法,因为我想在其中实现它界面,以便我可以调用它一次,然后只需要在我的代码的其余部分引用它,每当我想要进行互联网连接检查。这是检查的代码:

class InternetConnectionCheck
{
    public bool CheckForInternetConnection()
    {
        try
        {
            using (var client = new WebClient())
            {
                using (var stream = client.OpenRead("http://www.google.com"))
                {
                    return true;
                }
            }
        }
        catch
        {
            return false;
        }
    }

}

} }

I want to implement this in one of my interfaces 我想在我的一个接口中实现它

Interfaces only allow you to declare what properties, methods, and events an implementer must have. 接口只允许您声明实现者必须具有的属性,方法和事件。 An interface method cannot have a body. 接口方法不能有正文。 In your example, ApplicationManager needs to implement the checkConnection() method required by the IApplicationManager interface. 在您的示例中, ApplicationManager需要实现IApplicationManager接口所需的checkConnection()方法。

To call a method on one class from another, it must either be static or you must instantiate an instance of that class. 要从另一个类调用一个类的方法,它必须是静态的,或者必须实例化该类的实例。

Generally speaking, if a method does not refer to any instance members of its class, it should be static (there's not much point of it being non-static). 一般来说,如果一个方法没有引用其类的任何实例成员,那么它应该是静态的(没有太多的点是非静态的)。

A few other tips based on your code: 基于您的代码的一些其他提示:

  1. Your interface should probably be in its own file, rather than be a child of ApplicationManager . 您的界面应该在自己的文件中,而不是ApplicationManager的子ApplicationManager
  2. checkConnection should be CheckConnection to conform with C# norms. checkConnection应该是CheckConnection以符合C#规范。
  3. You can omit == true in a conditional and use ! 您可以在条件中省略== true并使用! instead of == false , as in if (CheckForInternetConnection()) and if (!CheckForInternetConnection()) . 而不是== false ,如if (CheckForInternetConnection())if (!CheckForInternetConnection()) Better yet, just use an else instead of testing for the false condition. 更好的是,只需使用else而不是测试错误情况。 (As @Servy pointed out, you're making two separate calls in your example, which is not what you want). (正如@Servy指出的那样,你在你的例子中进行了两次单独的调用,这不是你想要的)。

The method uses no instance state, so the method is conceptually static . 该方法不使用实例状态,因此该方法在概念上static

It doesn't make sense as being an instance method. 作为实例方法没有意义。 Making it an instance method would be implying that the method uses some state of the object, and requires anyone wishing to perform the method to have an instance of the type on hand in order to perform the operation, even though you don't actually plan to use that object for anything. 使其成为实例方法将暗示该方法使用对象的某种状态,并且要求任何希望执行该方法的人拥有该类型的实例以便执行操作,即使您实际上没有计划使用该对象的任何东西。

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

相关问题 C#:如何从派生类的静态方法调用基类的静态方法? - C#: How do I call a static method of a base class from a static method of a derived class? 如何从 C# 中的静态方法调用非静态方法? - How do I call a non-static method from a static method in C#? 如何从C#中的另一个类引用方法? - How do i reference a method from another class in C#? 如何在C#中的非静态方法中调用静态方法 - how to call static method inside non static method in c# 如何在C#中的另一个方法中从同一类中调用一个方法? - How to call a method from same class in another method in C#? 如何在C#中的另一个类之外调用非静态方法? - How can I call a non-static method out of another class in C#? 如何在C#中调用基类的显式接口方法? - How do I call an explicit interface method on a base class in C#? Selenium C#如何从另一个类调用方法,两个类都从基类继承 - Selenium C# How do I call a method from another class both classes inherit from a base class 如何在不破坏客户端的情况下向C#接口中的方法添加参数? - How do I add a parameter to a method in my interface in C# without breaking clients? 如何在不调用重写的情况下从基类中的另一个方法调用基虚方法? - How do I call a base virtual method from another method in the base class without calling the overrides?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM