【问题标题】:Call method in java without object or class name在没有对象或类名的java中调用方法
【发布时间】:2021-01-06 20:00:19
【问题描述】:
Button myButton = (Button) findViewById(R.id. my_button)

据我所知,在 java 中所有代码都必须在类体上,也就是说,如果我们调用一个方法,我们必须通过对象调用它,或者如果它是静态方法,则通过其类名,如 bla.findViewByID(somemoreblass)

但在 android studio 我看到了这个Button myButton = (Button) findViewById(R.id. my_button)

谁能解释它是如何工作的?

【问题讨论】:

  • findViewById 是声明 mybutton 的类的静态方法,或者它是 import static
  • 这个用户名很酷
  • @aran 我想是的
  • @fantaghirocco 你的意思是它是 Button 类的静态方法吗?抱歉,我在 rn,所以无法将 Button 突出显示为代码

标签: java android-studio


【解决方案1】:

findViewById()Activity 类中的一个方法,如果您位于 Activity 中,则您将从该类中扩展。就像在类或父类中定义的任何方法一样,可以在不使用类名或引用的情况下调用此方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-17
    相关资源
    最近更新 更多