【问题标题】:Call method in class from another class in C#从 C# 中的另一个类调用类中的方法
【发布时间】:2015-06-09 11:36:45
【问题描述】:

当我尝试从另一个类(不是主类)调用方法时遇到一个小问题。这是我的测试代码:

using System;

namespace ConsoleApplication3
{
    public class Program
    {
        static void Main(string[] args)
        {
            Class1 cl = new Class1();
            cl.TestMethod();
        }
    }

    public class Class1
    {
        public string TestMethod()
        {
            return "test";
        }
    }

    public class Class2
    {
        Class1 cl = new Class1();
        cl.TestMethod(); //Error here
    }
}

在 Class2 中调用 TestMethod 应该怎么做?

【问题讨论】:

    标签: c# methods call


    【解决方案1】:

    有一个简单的解决方案。就像你用 Math.Abs​​() 做的一样; 你应该使用 static 方法,

    using System;
    
    namespace ConsoleApplication3
    {
        public class Program
        {
            static void Main(string[] args)
            {
                Class1 cl = new Class1();
                cl.TestMethod();
                C_Auxilary.F_TestMeth();   
            }
        }
    
        public class Class1
        {
            public string TestMethod()
            {
                return "test";
            }
        }
    
        public class Class2
        {
            Class1 cl = new Class1();
            cl.TestMethod(); //Error here
        }
    }
    
    namespace ConsoleApplication3
    {
        public static class C_Auxilary
        {
            public static void F_TestMeth()
            {
                ;
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      您不能将方法调用到类主体中。指定方法,例如,将您的代码放入 Class2 构造函数中,如下所示:

      using System;
      
      namespace ConsoleApplication3
      {
          public class Program
          {
              static void Main(string[] args)
              {
                  Class1 cl = new Class1();
                  cl.TestMethod();
              }
          }
      
          public class Class1
          {
              public string TestMethod()
              {
                  return "test";
              }
          }
      
          public class Class2
          {
              Class1 cl = new Class1();
      
              public Class2()
              {
                  cl.TestMethod();
              }
          }
      }
      

      【讨论】:

        【解决方案3】:

        您已经定义了第二个类,它需要一个方法或在其中的构造函数才能使其工作。将它包裹在实例化周围,它将可以访问:)

        【讨论】:

          【解决方案4】:

          我认为您将类与方法混淆了。

          方法是一个子例程,是一段代码,可以从代码的其他部分调用并按顺序运行。

          类是一种数据类型。您创建一个类的特定实例,然后访问它的属性或调用它的方法。

          所以 Main 是一个方法,而不是一个类。具体是程序类的方法。

          在一个类中,所有可执行代码都必须在一个方法中。此规则的一个(某种)例外是您可以在方法之外初始化成员。这就是你对c1 = new Class1() 所做的。当您使用 new 创建类时,这些语句会隐式执行。

          要调用 class1 方法,您必须在 class2 上声明一个方法来执行它。l,或者定义一个构造函数来调用它。然而,拥有一个做很多工作的构造函数是非常糟糕的做法。构造应该只做一些事情,比如初始化类运行所需的成员。

          【讨论】:

            【解决方案5】:

            您不能从方法、构造函数或属性外部调用方法 将第二类更改为:

            public class Class2
            {
                public Class2
                {
                   Class1 cl = new Class1();
                   cl.TestMethod(); //Error here
                }}
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2010-11-02
              • 2020-10-20
              • 2014-01-31
              • 2017-02-03
              相关资源
              最近更新 更多