Using “this” with class name

前端 未结 7 1490
暗喜
暗喜 2020-11-30 18:19

I am doing Android programming and was learning about Intents, when I saw a constructor that, to my C# trained mind, seemed funky. The call was:

Intent myI         


        
7条回答
  •  春和景丽
    2020-11-30 18:44

    .this
    

    is used in nested classes to refer to the current instance of the enclosing class, since the `this' keyword refers to the nest class instance.

    public class Siht {
        class NestedSiht {
            void demoThis() {
                System.err.println("this' is an instance of: " + 
                                this.getClass().getName());
                System.err.println("Siht.this' is an instance of: " +
                                Siht.this.getClass().getName());
            }
        }

    void demoThis() {
        new java.lang.Object() {
            void demoThis() {
                System.err.println("`this' is an instance of: " + 
                                this.getClass().getName());
                System.err.println("`Siht.this' is an instance of: " +
                                Siht.this.getClass().getName());
            }
        }.demoThis();
        new NestedSiht().demoThis();
    }
    
    public static void main(String [] args) {
        new Siht().demoThis();
    }
    

    }

提交回复
热议问题