静态方法不可用 [英] static method not available

查看:77
本文介绍了静态方法不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我有一个静态类(CL),一个主类(Program)和一个简单类(cl1).

在静态类中,我有一个称为disply()的方法.
当我尝试在cl1中访问此方法时,我可以很好地做到这一点...但是,当我尝试在cl1类中访问它时,它不显示它.所有这三个类都在一个命名空间中.

请查看代码并帮助我...

Hello,

I have a static class(CL), one main class(Program) and a simple class(cl1).

I static class i have a method called disply().
When i am trying to access this method in cl1, i can very well able to do it...However when i tried to access it in class cl1, it does not shows it. All the three classes are in one namespace.

Please see the code and help me...

class Program
  {
      static void Main(string[] args)
      {
          cl.display();    //works !!
      }
  }

  public static class cl
  {
      public static string display()
      {
          return "hi";
      }
  }


  public class cl1
  {
           cl.display();    //DOES NOT WORKS..display function is not  //available....why !!
  }

推荐答案

除非您不正确地粘贴了代码,否则问题是您试图在公共类cl1中的方法定义之外调用方法.这是它的外观:

Unless you have improperly pasted your code, the issue is that you are trying to call a method outside of a method definition in your public class cl1. Here is the way it should look:

public class cl1
{
   public string CallDisplay()
   {
      return cl.display();
   }
}


cl1中的cl.display();不在方法主体内.
Program class中的cl.Display();起作用是因为它是Main方法的一部分.
您应该更改cl1,使其看起来像这样:
the cl.display(); in cl1 is not inside a method body.
cl.Display(); in your Program class works because it is part of the Main method.
You should change cl1 so it looks something like this:
public class cl1
{
    public void SomeMethod()
    {
        cl.display(); // Now works fine :)
    }
}


这篇关于静态方法不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆