为什么静态方法只允许调用静态方法而不是非静态方法

发布于 2024-12-11 01:32:50 字数 879 浏览 0 评论 0原文

可能的重复:
为什么我只能从静态访问静态成员功能?

当我尝试从静态方法内部调用普通方法时,出现错误:

非静态字段、方法或属性需要对象引用

因此这意味着我需要创建该类的对象,然后调用非静态方法。如果我想直接调用该方法,那么我必须将该方法声明为静态。

但是,在这种情况下,调用方法和被调用方法属于同一个类。那么为什么我需要在从静态方法调用时创建一个对象,而我可以从非静态方法调用非静态方法。

例如:

class Program
    {
        //public void outTestMethod(int x,out int y)
        //{
        //    y = x;
        //}
        static void Main(string[] args)
        {
            int a = 10;
            int b = 100;

            outTestMethod(a,out b); 
        }

        private  void outTestMethod(int x, out int y)
        {
            y = x;
        }
    }

错误:非静态字段、方法或属性需要对象引用

Possible Duplicate:
Why can I only access static members from a static function?

While I was trying to call a normal method from inside a static method I got the error:

An object reference is required for the non-static field, method, or property

So this means I need to create the object of the Class and then call the nonstatic method. If I want to call the method directly then I have to declare that method as Static.

But , in this scenario , the calling method and called method belong to same class. So Why do I need to create an object while calling from a Static Method , while I can call a non-static method from non static method.

Ex:

class Program
    {
        //public void outTestMethod(int x,out int y)
        //{
        //    y = x;
        //}
        static void Main(string[] args)
        {
            int a = 10;
            int b = 100;

            outTestMethod(a,out b); 
        }

        private  void outTestMethod(int x, out int y)
        {
            y = x;
        }
    }

Error:An object reference is required for the non-static field, method, or property

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

2024-12-18 01:32:50

静态方法可以调用实例方法 - 但您需要有一个实例来调用它们。该实例具体来自哪里并不重要,例如:

int a = 10;
int b = 100;
Program program = new Program();
program.outTestMethod(a,out b);

实例方法与类型的特定实例相关联,而静态方法与整个类型相关联 - 对于其他类型的成员也是如此。因此,要调用实例方法,您需要知道您对哪个实例感兴趣。例如,拥有:

string name = Person.Name;

因为您需要知道哪个人是 没有意义的你在谈论:

Person person = FetchPersonFromSomewhere();
string name = person.Name;

……这更有意义。

通常实例方法使用或修改实例的状态

Static methods can call instance methods - but you need to have an instance on which to call them. It doesn't matter where that instance comes from particularly, so for example:

int a = 10;
int b = 100;
Program program = new Program();
program.outTestMethod(a,out b);

Instance methods are associated with a particular instance of the type, whereas static methods are associated with the overall type instead - and the same is true for other kinds of members. So to call an instance method, you need to know which instance you're interested in. For example, it would be meaningless to have:

string name = Person.Name;

because you need to know which person you're talking about:

Person person = FetchPersonFromSomewhere();
string name = person.Name;

... that makes much more sense.

Typically instance methods use or modify the state of the instance.

情深如许 2024-12-18 01:32:50

这样考虑吧。

静态方法是一组电梯外面的按钮。任何人都可以看到它并推动它,并使某些事情发生(即其中一部电梯将到达该楼层)。

非静态方法是特定电梯内的按钮。他们操纵那部电梯(而不是其他电梯)。

Consider it this way.

A static method is the button outside a bank of elevators. Anyone can see it and push it, and make something happen (i.e. one of the elevators will arrive at that floor).

The non-static methods are the buttons inside a specific elevator. They manipulate THAT elevator (and none of the others).

美男兮 2024-12-18 01:32:50

非静态方法也称为实例方法。这意味着(通常)有一块数据,特定于该方法所操作的实例(对象)。

您不能从静态方法调用非静态或实例方法,因为它不知道要操作哪个实例或对象。

A non-static method is also called an instance method. This means there (usually) is a chunk of data, specific to the instance (object) that the method operates on.

You can't call a non static or instance method from a static method because it wouldn't know which instance or object to operate on.

冷情妓 2024-12-18 01:32:50

因为没有实例可以调用该方法。您应该创建另一个类并对其进行测试:

class Program
{
    static void Main(string[] args)
    {
        int a = 10;
        int b = 100;
        Test testclass = new Test();
        testclass.outTestMethod(a,out b); 
    }
}

class Test
{

    public void outTestMethod(int x, out int y)
    {
        y = x;
    }
}

Because there is no instance to call the method from. YOu should create possibly another class and test on that:

class Program
{
    static void Main(string[] args)
    {
        int a = 10;
        int b = 100;
        Test testclass = new Test();
        testclass.outTestMethod(a,out b); 
    }
}

class Test
{

    public void outTestMethod(int x, out int y)
    {
        y = x;
    }
}
一梦浮鱼 2024-12-18 01:32:50

你明白实例方法和静态方法的区别吗?

实例方法可以访问 this 对象,即使它没有作为参数传入,事实上,它就像是框架为您传递的类的相同类型的不可见参数。

静态方法没有 this 对象,并且无法调用实例方法,因为它没有任何内容以不可见的方式传递给该 this...

听起来像个笑话但这就是我的看法:)

do you understand the difference between instance method and static method?

an instance method has access to the this object even if it's not passed in as parameter, in fact it's like if it was an invisible parameter of the same type of the class passed for you by the framework.

a static method does not have that this object and cannot call instance methods because it does not have anything to pass for that this in the invisible way...

sounds like a joke but this is the way I see it :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文