非静态成员在静态方法....怎么可能? [英] non-static member in static method....how is it possible ?

查看:76
本文介绍了非静态成员在静态方法....怎么可能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我了解这种情况...我早些时候问过这个问题,但不幸的是没有得到合理的答案...

Hi , please help me understanding this situation...i asked the issue earlier but unfortunately didnt get the justified answer...

class Program
{
    int i;
    static void Main(string[] args)
    {
        cl obj = new cl();
      //  i=34;   <-- cant access since it is not static.
    }
}
 
class cl
{
    public  void display()
    {
    }
}





我的问题是在void main方法(不允许使用非静态成员)中,我怎么能创建"cl"类的实例(非静态)? /> 为什么不给出错误:
问题





My problen is that in the void main method (which does not allows non-static members in it, how come am i able to create an instance of the ''cl'' class (which is non-static)???
Why does it NOT give an error :
"An object reference is required for the non-static field, method, or property."ked the question

推荐答案

您可以在任何地方创建对象.在这种情况下,obj对于Main是局部的,并且仅在该方法的生存期内存在.您不会得到错误,因为创建c1不需要来自Program的任何实例级别信息.您无法访问i,因为它是Program中的实例变量.

在某种程度上,obj 是静态的,因为它没有绑定到实例(仅绑定到静态上下文中的方法).您从Main查找的类c1不是实例级项(在这种意义上,类始终是静态"的,尽管请注意,这并不是静态类"的意思).

我不是很明白这个问题,因为它很明显.
You can create an object anywhere. In this case, obj is local to Main and only exists for the lifetime of that method. You don''t get an error because creating a c1 doesn''t require any instance level information from Program. You can''t access i because it is an instance variable in Program.

In a way, obj is static in that it is not bound to an instance (only to a method which is in a static context). What you are looking up from Main, the class c1, is not an instance level item (classes are always ''static'' in this sense, though note that that is not what ''static class'' is used to mean).

I don''t really understand the question, in that it is obvious.


您可以用静态或非静态方法实例化类(创建其对象),如您所见以上.但是,(Program类的)实例变量i只能存在于从该类创建的对象中,因此您需要具有以下内容:
You can instantiate (create an object of) a class in a static or non-static method, as you see above. However the instance variable i (of the Program class) can only exist within an object created from that class, so you would need to have something like:
static void Main(string[] args)
{
    Program obj = new Program();
    obj.i = 34;   // can access instance variable through the object
}


如果将i设置为static,则可以直接访问它,但是在这种情况下,该类的所有实例都只有一个这样的变量,因此请在一个位置进行更改,然后在各处进行更改.


我建议您花更多时间阅读有关类,对象等的章节中的C#文档,以获取更详细的描述.


If i is made static then you can access it directly, but in that case there is only one such variable for all instances of the class, so change it in one place and it is changed everywhere.


I would suggest you spend some more time reading your C# documentation in the section(s) concerning classes, objects etc. for more detailed descriptions.


最简单的说法是,因为您正在创建一个对象的实例(cl).例如,您可以调用它来更新/实例化i的值:

In the simplest terms, because you are creating an instance of the object (cl). You could, for instance, call this to update/instantiate the value of i:

Program p = new Program();
p.i = 34;


这篇关于非静态成员在静态方法....怎么可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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