静态主要方法的工作. [英] working of static main methos.

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

问题描述

请帮助我了解这种情况...

Hi , please help me understanding this situation...

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"类的实例(非静态)? /> 为什么不给出错误:
非静态字段,方法或属性需要对象引用."

[edit]添加了代码块-OriginalGriff [/edit]




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."

[edit]Code block added - OriginalGriff[/edit]

推荐答案

简单:Main方法始终是静态的-其中只有一种(或者框架不会"(不知道在启动时要运行哪个实例),因此运行正常.但是,它是静态的,这意味着它没有附加到任何实例,因此也没有暗示或指定的this指针.结果,您不能使用"i",因为它不是静态属性,并且需要特定的实例才能为您提供this指针.

执行以下操作时:
Simple: The Main method is always static - there is only ever one of these (or the framework wouldn''t know which instance to run on startup), so it runs fine. But, it is static, which means it is not attached to any instance, and so has no this pointer, either implied or specified. As a result, you cannot use "i" because it is not a static property, and requires a specific instance to give you the this pointer.

When you do this:
cl obj = new cl();

您将创建"cl"类的实例,该类由Main中一个称为"obj"的变量引用.因为它是一个实例,所以它可以具有所需的所有属性,并且可以一直保留到超出范围为止-在这种情况下,代码将到达Main方法的末尾.

如果您无法执行此操作,则您将无法实例化任何类,并且您的程序必须非常简短,而且很无聊!

you create an instance of the "cl" class, referred to by a variable within Main, called "obj". Because it is an instance, it can have all the properties it needs, and it persisted until it goes out of scope - in this case by the code reaching the end of the Main method.

If you couldn''t do this, you could never instantiate any class, and your program would have to be very short, and boring!


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


首先,您需要了解静态和非静态成员.非静态方法称为实例方法.它只能在实例上调用,不能在类(如static)上调用,并且可以在运行时调用时访问类或结构的具体实例.如何?它只是具有在实际调用期间传递的类/结构类型的隐藏参数.此参数称为"this",可以显式使用,例如this.MyField.

现在,函数Program.Main是静态的(这是创建入口点的唯一方法;如果还没有任何实例,怎么办?),类Program不必是静态的(但可以).

因此,我可以建议您一个简单的模式:

First, you need to understand static and non-static members. Non-static method is called instance method. It can be called only on instance, not the class like static and is given access to a concrete instance of the class or structure as it appears during run-time call. How? It simply has a hidden parameter of the class/structure type passed during actual call. This parameter is called "this" and can be used explicitly, such as this.MyField.

Now, the function Program.Main is static (this is the only way to make an entry point; how else if there are no any instances yet?), the class Program does not have to be static (but it can).

So, I can suggest you a simple pattern:

class Program {

   void Run() { //instance method, not a static one
       //access any instant or static members here
   }

   static void Main(string[] args) {
       new Program().Run(); //here you go from static to instance way
   }

}



—SA



—SA


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

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