pau*_*890 0 java methods android
我是java/android的新手,非常感谢这方面的帮助.我希望我的java方法能够为参数接受多种类型.例如,变量myButton可以是ImageButton,也可以只是Button.myFunction中的代码对myButton是ImageButton还是Button都有效.
if (condition) {
ImageButton myButton = (ImageButton) findViewById(R.id.myButtonID);
myFunction(myButton);
} else {
Button myButton = (Button) findViewById(R.id.myButtonID);
myFunction(myButton);
}
public void myFunction(Button or ImageButton myButton) {
....identical code for myButton which could be an ImageButton or Button....
}
Run Code Online (Sandbox Code Playgroud)
我显然可以写出2个参数类型的功能,并在适用时调用它,但这似乎是浪费,必须有一个更快的方法.我怎样才能做到这一点?谢谢你的帮助.
| 归档时间: |
|
| 查看次数: |
432 次 |
| 最近记录: |