成员变量和本地方法变量可以具有相同的名称吗? [英] Can member variable and local method variable have the same name?

查看:76
本文介绍了成员变量和本地方法变量可以具有相同的名称吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何做到这一点?

class Test {
    private int var1;

    public Test(int var1) {
        var1 = var1; //set the member variable to what was passed in
    }
}

我确信有一个非常明显的答案。它现在正在逃避我。

I'm sure there's a very obvious answer. It's just escaping me right now.

推荐答案

是的,他们可以共享相同的名称。但是,要引用实例变量,您需要使用前缀:

Yes, they can share the same name. However, to reference the instance variable, you need to use the this prefix:

public Test(int var1) {
    this.var1 = var1;
}

这篇关于成员变量和本地方法变量可以具有相同的名称吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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