简体   繁体   English

java中的这个关键字

[英]this keyword in java

I am current reading a java tutorial from Oracle in the inner class section. 我目前正在内部类部分阅读Oracle的Java教程。

Please refer to this link 请参阅此链接

There is some code that I do not understand in the tutorial. 在教程中有一些我不理解的代码。

Can somebody please explain to me how this code below in the DataStructure class works? 有人可以向我解释下面的DataStructure类中的代码如何工作?

DataStructureIterator iterator = this.new EvenIterator();

Should the outer class not be before DataStructureIterator iterator and this.new EvenIterator() like below: 外部类应该this.new EvenIterator() DataStructureIterator iteratorthis.new EvenIterator()如下所示:

DataStructure.DataStructureIterator iterator = DataStructure.this.new EvenIterator();

I have searched around for a while but I have not found any answer. 我已经搜索了一段时间,但我没有找到任何答案。

The declaration DataStructure.DataStructureIterator iterator = DataStructure.this.new EvenIterator(); 声明DataStructure.DataStructureIterator iterator = DataStructure.this.new EvenIterator(); is valid, but redundant in that method's context. 是有效的,但在该方法的上下文中是多余的。

Consider this scenario, where there are conflicting inner classes 考虑这种情况,其中存在冲突的内部类

public void printEven() {
    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            //Makes EvenIterator point to DataStructure's implementation
            DataStructureIterator itr = DataStructure.this.new EvenIterator();
        }

        class EvenIterator implements DataStructureIterator {

            @Override
            public boolean hasNext() {
                return false;
            }

            @Override
            public Integer next() {
                return null;
            }

            @Override
            public void remove() {
            }

        }
    });
}

As you can see the anonymous class Runnable has inner class named EvenIterator (which has same name as Outer class's inner class). 正如您所看到的,匿名类Runnable具有名为EvenIterator内部类(其名称与Outer类的内部类相同)。 So writing just 所以写作只是

DataStructureIterator itr = this.new EvenIterator(); //OR DataStructureIterator itr = new EvenIterator();

will refer to Runnable 's EvenIterator . 将引用RunnableEvenIterator To point to DataStructure 's EvenIterator , you might want to write 指向DataStructureEvenIterator ,你可能想要写

DataStructureIterator itr = DataStructure.this.new EvenIterator();

Which says, I want EvenIterator to be created on current instance of DataStructure , not current instance of Runnable 它说,我想EvenIterator要在目前的情况下创建DataStructure ,而不是当前实例Runnable

"this keyword" refer to the scope of an object class so : “this keyword”指的是对象类的范围,所以:

public class Test {
     public void method1(){
         this. //this in here refer to "Test" class
     }

     public class InnerTest {
        public void method2(){
           this. //this in here refer to "InnerTest" class
        }
     }
}

now take a look at "this keyword" in this example : 现在看一下这个例子中的“this keyword”:

public class Test {
     public void method1(){

         InnerTest innerTest = this.new InnerTest(); //within the scope of Test class, there's a inner class call InnerTest. create an instance of it !
         //OR
         TestInterface innerTest2 = this.new InnerTest(); //within the scope of Test class, there's a inner class call InnerTest. create an instance of it !
     }

     public class InnerTest implements TestInterface{
        public void method2(){
           Test.this.method1(); //within the scope of "Test" class call the method method1
        }
     }
}

take note that you can use this keyword to chain the constructor of a class as well. 请注意,您也可以使用此关键字来链接类的构造函数。 take a look at this example: 看看这个例子:

public class Test {

    public Test() {
        this(0);
    }

    public Test(int r) {
        // Do Something Here
    }
}

Conclusion: 结论:

"this" keyword acts like a pointer to the scope of a class and using "this" you can point to a specific class and do whatever you want. “this”关键字就像一个指向类范围的指针 ,使用“this”,你可以指向一个特定的类,并做任何你想做的事情。

You can also use "this" keyword to chain class constructors as well ! 您也可以使用“this”关键字来链接类构造函数

public class DataStructure {
    /** omitted */

    public void printEven() {
       //#1
       DataStructureIterator iterator = this.new EvenIterator();
       /** Omitted */
    }

    //#2
    private class EvenIterator implements DataStructureIterator {
      /** Omitted */
    }
}

So this is the bare-bone problem. 所以这是一个愚蠢的问题。

Lets see what we have: 让我们看看我们有什么:

  1. A class DataStructure that has a non-static method printEven . 一类DataStructure具有非静态方法printEven the this keyword in this class will always represent the current instance/object of this class: DataStructure . this中的this关键字将始终表示此类的当前实例/对象: DataStructure

  2. An inner class, that is private. 内部类,是私有的。 This means this class is not visible outside. 这意味着此课程在外面不可见。 It is not static that means you need an instance of outer-class to be able to instanciate this class-member (aka inner class). 它不是静态的,这意味着你需要一个外部类的实例才能实现这个类成员(也就是内部类)。

So, we want to create an instance of EvenIterator , we will need a. 所以,我们要创建一个EvenIterator实例,我们需要一个。 an instance of outer class, b. 外类的一个实例,b。 call a constructor of inner class. 调用内部类的构造函数。

  DataStructureIterator iterator = this.new EvenIterator();
                                     |        `---- Call to constructor
                                     `-------------- instance of outer class 

Doing DataStructure.this in DataStructure is redundant. DataStructure.this中执行DataStructure是多余的。 Because we know that we are in the scope of DataStructure so just this will do. 因为我们知道我们属于DataStructure的范围,所以this做。 However, if you are in inner class EvenIterator , and you want to explicitely access outer class's instance you will have to to do DataStructure.this , because in inner class this will mean the instance of EvenIterator . 但是,如果您在内部类EvenIterator ,并且您希望明确访问外部类的实例,则必须执行DataStructure.this ,因为在内部类中this将表示EvenIterator的实例。

The keyword this is actually written in the outer class, not the inner class, in your specific example. 在特定示例中,关键字this实际上是在外部类中编写的,而不是内部类。 Therefore, it's not ambiguous in any way - it can only refer to the DataStructure instance. 因此,它不以任何方式模糊 - 它只能引用DataStructure实例。 It's therefore unnecessary (but legal) to qualify it as DataStructure.this in this case. 因此,在这种情况下,将其限定为DataStructure.this是不必要的(但是合法的)。 If you used this within the inner class, and you intended it to refer to the DataStructure instance, you would have to write DataStructure.this . 如果您在内部类中使用this ,并且您希望它引用DataStructure实例,则必须编写DataStructure.this

According to the Java Language Specification 15.8.3 - 根据Java语言规范15.8.3 -

The type of this is the class C within which the keyword this occurs. 其类型是C类,其中出现关键字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM