简体   繁体   English

非静态字段,方法或属性DoStuff(int,int)'需要对象引用

[英]An object reference is required for the non-static field, method, or property DoStuff(int, int)'

using System;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int number1,number2;
            number1 = int.Parse(Console.ReadLine());
            number2 = int.Parse(Console.ReadLine());
            Console.WriteLine("the sum of numbers are " +" "+ (number1 + number2));
            Console.ReadKey();
            DoStuff(number1,number2);
        }

        public int DoStuff(int num1,int num2)
        {
            int result = num1 + num2;
            return result;
        }

    }
}

What am I doing wrong? 我究竟做错了什么?

You are not allowed to call a non-static method inside a static method. 不允许在静态方法内调用非静态方法。 If you want to invoke then you need a object reference. 如果要调用,则需要一个对象引用。 This is because the static methods cannot be instantiate. 这是因为静态方法无法实例化。 So the method DoStuff should be static, hence its signature may looks like this: 因此,方法DoStuff应该是静态的,因此其签名可能如下所示:

public static int DoStuff(int num1,int num2)
{
    int result = num1 + num2;
    return result;
} 

I have one more suggestion for you to improve your code; 我还有另一条建议,建议您改善程式码; it is nothing but use int.TryParse instead for simple .Parse . 它只是使用int.TryParse代替简单的.Parse Which helps you to handle FormatException too. 这也可以帮助您处理FormatException。 So the complete code will looks like: 因此完整的代码如下所示:

class Program
{
    static void Main(string[] args)
    {
        int number1, number2;
        if (int.TryParse(Console.ReadLine(), out number1) && int.TryParse(Console.ReadLine(), out number2))
        {
            Console.WriteLine("the sum of numbers are :{0}", DoStuff(number1, number2));
        }
        else
            Console.ReadKey();
    }
    public static int DoStuff(int num1, int num2)
    {
        int result = num1 + num2;
        return result;
    }

}

From a static function you can call only static functions. 在静态函数中,您只能调用静态函数。 So change your DoStuff function to be static: 因此,将DoStuff函数更改为静态函数:

public static int DoStuff(int num1,int num2)
{
    int result = num1 + num2;
    return result;
}

For more info: Static classes 有关更多信息: 静态类

A non-static member belongs to an instance. 非静态成员属于实例。 It's meaningless without somehow resolving which instance of a class you are talking about. 如果不以某种方式解决您正在谈论的类的实例,这是毫无意义的。 In a static context, you don't have an instance, that's why you can't access a non-static member without explicitly mentioning an object reference. 在静态上下文中,您没有实例,这就是为什么如果不显式提及对象引用就无法访问非静态成员的原因。

In fact, you can access a non-static member in a static context by specifying the object reference explicitly: 实际上, 可以通过显式指定对象引用来在静态上下文中访问非静态成员:

class HelloWorld {
   int i;
   public HelloWorld(int i) { this.i = i; }
   public static void Print(HelloWorld instance) {
      Console.WriteLine(instance.i);
   }
}

var test = new HelloWorld(1);
var test2 = new HelloWorld(2);
HelloWorld.Print(test);

Without explicitly referring to the instance in the Print method, how would it know it should print 1 and not 2? 在不显式引用Print方法中的实例的情况下,它将如何知道应打印1而不是2?

You are using DoStuff as a static method. 您正在使用DoStuff作为静态方法。

You should do the following: 您应该执行以下操作:

...
Console.ReadKey();
var program = new Program();
program.DoStuff(number1, number2);
...

So create an instance of your class and call the method upon it. 因此,创建您的类的实例并在其上调用方法。

If you do want to use the method in a static way, you should declare it statically: 如果确实要以静态方式使用该方法,则应静态声明它:

public static int DoStuff(int num1,int num2)
{
    int result = num1 + num2;
    return result;
} 

See the "static" keyword after "public". 请参阅“公共”之后的“静态”关键字。

From C# Specifications: 从C#规范:

1.6.6.3 Static and instance methods 1.6.6.3静态方法和实例方法

A method declared with a static modifier is a static method. 用static修饰符声明的方法是静态方法。 A static method does not operate on a specific instance and can only directly access static members. 静态方法不会在特定实例上运行,只能直接访问静态成员。 A method declared without a static modifier is an instance method. 没有静态修饰符声明的方法是实例方法。 An instance method operates on a specific instance and can access both static and instance members. 实例方法在特定实例上运行,并且可以访问静态成员和实例成员。 The instance on which an instance method was invoked can be explicitly accessed as this. 这样可以显式访问在其上调用了实例方法的实例。 It is an error to refer to this in a static method. 在静态方法中引用它是错误的。

Your Main method is static, so it can only access static members. 您的Main方法是静态的,因此它只能访问静态成员。 Solution is to change: 解决方案是更改:

public static int DoStuff(int num1,int num2)

Either you can stuff the Method with static as show in below or 您可以使用如下所示的静态方法填充方法,或者

public static  int DoStuff(int num1,int num2)
 {
    int result = num1 + num2;
    return result;
 }

Static methods :These are called with the type name. 静态方法 :使用类型名称调用这些方法 No instance is required—this makes them slightly faster. 不需要实例,这使它们速度稍快。 Static methods can be public or private. 静态方法可以是公共的或私有的。

Info: Static methods use the static keyword, usually as the first keyword or the second keyword after public. 信息:静态方法使用static关键字,通常用作public之后的第一个关键字或第二个关键字。 Warning: A static method cannot access non-static class level members. 警告:静态方法无法访问非静态类级别的成员。 It has no "this" pointer. 它没有“ this”指针。

Instance: An instance method can access those members, but must be called through an instantiated object. 实例:实例方法可以访问那些成员,但是必须通过实例化的对象进行调用。 This adds indirection. 这增加了间接性。

For Example : 例如

class Program
   {
    static void MethodA()
    {
        Console.WriteLine("Static method");
    }

    void MethodB()
    {
        Console.WriteLine("Instance method");
    }

    static char MethodC()
    {
        Console.WriteLine("Static method");
        return 'C';
    }

    char MethodD()
    {
        Console.WriteLine("Instance method");
        return 'D';
    }

    static void Main()
    {
        //
        // Call the two static methods on the Program type.
        //
        Program.MethodA();
        Console.WriteLine(Program.MethodC());
        //
        // Create a new Program instance and call the two instance methods.
        //
        Program programInstance = new Program();
        programInstance.MethodB();
        Console.WriteLine(programInstance.MethodD());
        Console.ReadLine();
    }
}

暂无
暂无

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

相关问题 非静态字段,方法或属性ConsoleApplication5.Program.myFirst(int,int)要求对象引用 - An object reference is required for the non-static field, method, or property ConsoleApplication5.Program.myFirst(int, int) 错误1非静态字段,方法或属性'temperature_conversion.Program.Celsius(int)'需要对象引用 - Error 1 An object reference is required for the non-static field, method, or property 'temperature_conversion.Program.Celsius(int)' 非静态字段,方法或属性需要对象引用 - An object reference is required for the non-static field, method, or property 非静态字段,方法或属性需要对象引用 - An object reference is required for the non-static field, method, or property 非静态字段,方法或属性是否需要对象引用? - An object reference is required for the non-static field, method, or property? 非静态字段,方法或属性需要对象引用 - An object reference is required for the non-static field, method, or property 非静态字段,方法或属性(数据集)需要对象引用 - An object reference is required for the non-static field, method, or property (dataset) 错误:非静态字段,方法或属性需要对象引用 - Error: An object reference is required for the non-static field, method, or property 对象引用是必需的非静态字段,方法或属性错误 - An object reference is required non-static field, method, or property error 非静态字段,方法或属性需要对象引用 - Object reference is required for non-static field, method or property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM