什么"这&Q​​UOT;意味着? [英] What does "this" mean?

查看:157
本文介绍了什么"这&Q​​UOT;意味着?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  的Java this.method()与()的方法

我一直在阅读一些事情,做一些教程关于Android的Java,但我还是不明白这个的意思,就像下面code。

I've been reading some things and doing some tutorials about android java but I still dont understand what "this" means, like in the following code.

    View continueButton = this.findViewById(R.id.continue_button);
    continueButton.setOnClickListener(this);
    View newButton = this.findViewById(R.id.new_button);
    newButton.setOnClickListener(this);

另外为什么会在这个例子中,一个按钮没有与按钮,但与景观的定义,有什么区别呢?

Also why is it in this example that a button is not defined with Button but with View, what is the difference?

PS。伟大的网站!努力学习java和得到答案的配发搜索在这里!

ps. Great site!! trying to learn java and got ALLOT of answers by searching here!

推荐答案

关键字的引用了当前对象。它被用来传递的的对象的此的实例,并且更

The this keyword is a reference to the current object. It is used to pass this instance of the object, and more.

例如,这两个分配是平等的:

For example, these two allocations are equal:

class Test{

    int a;

    public Test(){
        a = 5;
        this.a = 5;
    }

}

有时候,你有一个隐藏字段,你要访问:

Sometimes you have a hidden field you want to access:

class Test{

    int a;

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

}

在这里,您指定的字段 A 与参数的值 A

关键字以同样的方式与方法。再次,这两个是相同的:

The this keyword works the same way with methods. Again, these two are the same:

this.findViewById(R.id.myid);
findViewById(R.id.myid);

最后,说你有一个类为MyObject,有一个方法,需要一个为MyObject参数:

Finally, say you have a class MyObject that has a method which takes a MyObject parameter:

class MyObject{

    public static void myMethod(MyObject object){
        //Do something
    }

    public MyObject(){
        myMethod(this);
    }

}

在最后这个例子中,你通过了当前对象的引用静态方法。

In this last example you passed a reference of the current object to a static method.

另外为什么会在这个例子中,一个按钮没有与按钮,但与景观的定义,有什么区别呢?

Also why is it in this example that a button is not defined with Button but with View, what is the difference?

在Android SDK中,一个按钮是的查看子类。您可以要求按钮查看中的查看按钮

In Android SDK, a Button is a subclass of View. You can request the Button as a View and cast the View to a Button:

Button newButton = (Button) this.findViewById(R.id.new_button);

这篇关于什么"这&Q​​UOT;意味着?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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