【问题标题】:java using this word is optional or not? [duplicate]java使用这个词是可选的吗? [复制]
【发布时间】:2015-03-09 15:02:04
【问题描述】:

在 Java 中是否使用关键字 this 可选?还是我必须使用它?当它不是可选的? 在以下代码中,无论我创建了多少 Employee 实例,它都不会影响我的应用程序。 print 方法打印 Employee 详细信息没有任何错误,即使详细信息不匹配。

public class Employee {
    String name;
    int Salary;
    int pension;
    String workPlace;
    String teleNo;
    int age;

    void printDetails(){
        System.out.println("Name is :  "+this.name );
        System.out.println("age is  :  "+this.age );
        System.out.println("WorkPlace is  :  "+this.workPlace );
        System.out.println("Salary is  :  "+Salary );
        System.out.println("Pension is  :  "+this.pension );
        System.out.println("Telephone No. is  :  "+this.teleNo );
        System.out.println("age is  :  "+Integer.toString(age) );
    }
}

public class Main extends Employee {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Employee obj=new Employee();
        obj.age=25;
        obj.name="Yasser";
        obj.pension=100_000;
        obj.teleNo="xxx_xxxx";
        obj.workPlace="Egypt";
        obj.Salary=1000000;

        obj.printDetails();
        Employee obj1=new Employee();
        obj1.age=29;
        obj1.name="asser";
        obj1.pension=100_000;
        obj1.teleNo="xxx_xxxx";
        obj1.workPlace="rgypt";
        obj1.Salary=2000000;
        obj1.printDetails();
    }
}

【问题讨论】:

  • 您可以通过谷歌搜索轻松找到更多信息:docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html
  • this 总是指类的当前实例。所以你不需要使用,除非有一个同名的方法局部变量,但你想使用你的实例变量,然后我们显式在变量名前加上this,告诉编译器我们需要使用实例变量而不是在这种情况下是一个局部变量

标签: java this keyword optional


【解决方案1】:

它始终是可选的除非您引用一个与局部变量同名的字段。例如:

class Sample
{
    int value;

    void method(int value)
    {
        this.value = value; //this is required
    }
}

如果不是这种情况,那么使用this 的行为与直接引用变量的行为相同。

【讨论】:

    【解决方案2】:

    this 在 Java 中是可选的,除非你有一个同名的局部变量或参数,在这种情况下,变量名将引用后者。

    public class Employee {
    
        private String name = "one";
    
        public void printDetails() {
            String name = "two";
            System.out.println(this.name); // "one"
            System.out.println(name); // "two"
        }
    }
    

    【讨论】:

      【解决方案3】:

      当您在具有相同名称的不同“作用域”中有变量时,您需要使用“this”。所以如果你有一个实例变量和一个方法中的一个变量调用的是同一个东西,你必须在实例变量前加上'this',否则局部变量会覆盖实例变量。

      否则它是可选的,是个人喜好的问题。

      有些人喜欢在代码中明确变量的范围 - 其他人认为这是混乱和不必要的。

      就我个人而言,如果可能的话,我的目标是不要为不同范围内的变量使用相同的名称。

      【讨论】:

        【解决方案4】:

        this 始终是可选的,除了引用在本地和全局名称相同的字段。例如:

        class test
        {
            int varname; //global 
        
            public void testMethod(int varname)
            {
                this.varname=varname; //this.varname refers to the declaration within the class, not the method (the global 'varname' variable)
        
            }
        

        正如其他人所提到的,documentation 总是值得一读。

        另外,this 指的是 current 对象。根据文档,这意味着 this 指的是 current 对象,其方法或构造函数正在被调用。因此,例如,如果您有一些代码声明了一个 Swing 组件并附加了一个动作监听器,那么该动作监听器中的代码将引用带有 this 关键字前缀的动作监听器:

        swingJButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                this.actionPerformed(e); // <-- refers to "new ActionListener()" object above
            } });
        

        为简单起见,除非必须,否则最好避免使用关键字。一些程序员更喜欢在不同的范围内使用相同的变量名——这在所谓的 setter 方法中相当常见,而另一些则不——这取决于偏好;无论您选择哪个,请注意 this 指的是。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-08-23
          • 2018-04-11
          • 1970-01-01
          • 1970-01-01
          • 2022-01-09
          • 2011-01-29
          • 1970-01-01
          相关资源
          最近更新 更多