类变量,成员变量和局部变量,全局变量之间的区别 [英] Difference between Class variable, Member variable, and Local variable, Global Variable

查看:75
本文介绍了类变量,成员变量和局部变量,全局变量之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要在`Class变量,Member变量,Local变量和Global变量之间进行分类?

Categorize between `Class variable, Member variable, Local variable and Global Variable?

推荐答案

在类定义中定义为静态的变量是类变量.

a variable defined static in class definition is class variable.

public MyClass
{
    static int a; // class variable
}

在函数(方法)中声明的变量是局部变量.

a variable declared in a function (Method) is local variable.

public class MyClass
{
    static void Main()
    {
        string name; //local variable
    }
}

在类定义中声明的变量,当实例化类时,这些变量将成为成员变量

a variable declared in a class definition, and when class is instantiated and those variables will be member variable

public class MyClass
{
    int a; // here they are local variable of class body.
    int b;
}

//create instance of class

MyClass mc = new MyClass();
mc.a = 10; //these are member variables
mc.b = 11;

这篇关于类变量,成员变量和局部变量,全局变量之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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