成员变量和局部变量有什么区别? [英] What is the difference between a member variable and a local variable?

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

问题描述

成员变量和局部变量有什么区别?

What is the difference between a member variable and a local variable?

它们是一样的吗?

推荐答案

成员变量是一个类型的成员并且属于该类型的状态.局部变量不是类型的成员,它代表本地存储而不是给定类型的实例的状态.

A member variable is a member of a type and belongs to that type's state. A local variable is not a member of a type and represents local storage rather than the state of an instance of a given type.

然而,这一切都非常抽象.这是一个 C# 示例:

This is all very abstract, however. Here is a C# example:

class Program
{
    static void Main()
    {
        // This is a local variable. Its lifespan
        // is determined by lexical scope.
        Foo foo;
    }
}

class Foo
{
    // This is a member variable - a new instance
    // of this variable will be created for each 
    // new instance of Foo.  The lifespan of this
    // variable is equal to the lifespan of "this"
    // instance of Foo.
    int bar;
}

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

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