[英]How to pass methods as parameters in Android
這可能是一個非常愚蠢的問題,甚至是不可能的。
我不確定是否可以將其作為方法調用,但是請繼續進行下去。 如何傳遞getCountryName(); 作為參數? 如下所示;
public static String Utils(Something something){
Geocoder geoCoder = new Geocoder(context, Locale.getDefault());
List<Address> address = null;
if (geoCoder != null) {
try {
address = geoCoder.getFromLocation(lat, lng, 1);
} catch (IOException e1) {
e1.printStackTrace();
}
if (address.size() > 0) {
return address.get(0).getCountryName(); // pass this as parameter (something)
}
}
return null;
}
然后這樣稱呼它,
Utils(getCountryName());
Utils(getCountryCode());
etc.
這個答案幫助我做到了。 您也可以嘗試。
方法不是Java中的一流對象,因此它們不能作為參數傳遞。 您可以在擴展了
Runnable
接口的匿名類中使用方法調用包裝。
您可以使用反射。
例如
Method[] methods = Address.class.getMethods();
for (Method method : methods){
if(method.getName().equals(param)){
return (String) method.invoke(address.get(0));
}
}
param
的類型為String
具有您要調用的方法的值。
編輯:使用getMethod(String)
完整示例
public static String Utils(String param){
Geocoder geoCoder = new Geocoder(context, Locale.getDefault());
List<Address> address = null;
if (geoCoder != null) {
try {
address = geoCoder.getFromLocation(lat, lng, 1);
} catch (IOException e1) {
e1.printStackTrace();
}
if (address.size() > 0) {
Method method = Address.class.getMethod(param);
if(method == null) return null;
try{
return (String) method.invoke(address.get(0));
}catch(ReflectiveOperationException e){
// Expected param maybe?
return null;
}
}
}
return null;
}
在Java中,這是您最終使用的語言。 您不能將方法或函數作為參數傳遞給函數。 如果能夠使用它,則從Java 8開始,您將擁有lambda表達式,該表達式是一種“匿名”函數。 試試吧。
http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html
在這個答案中,您有一個非常詳細的指南。 Java Pass方法作為參數
如果您不能使用java8,那么在該線程中會有更多信息以及某些模式
嘗試這個;
首先使用要作為參數傳遞的方法定義一個接口
public interface Callable {
public void getCountryName();
public void getCountryCode();
}
用方法實現一個類
class Method_passing implements Callable {
public void getCountryName() {
//do your task
}
public void getCountryCode() {
//do your task
}
}
這樣調用
Callable cmd = new Method_passing();
//This allows you to pass cmd as parameter and invoke the method call defined in the interface
public invoke( Callable callable ) {
cmd.getCountryName(); //return name etc..
cmd.getCountryCode();
}
您可以在adress類中實現getUtil函數,然后使用標識符執行您選擇的函數。
public class Address {
public static final int GET_COUNTRY_NAME = 1;
public static final int GET_COUNTRY_CODE = 2;
public String getUtil(int code){
switch(code){
case GET_COUNTRY_NAME: return this.getCountryName();
case GET_COUNTRY_CODE: return this.getCountryCode();
default: return null;
}
}
private String getCountryName(){
//do something
return "";
}
private String getCountryCode(){
//do something
return "";
}}
然后定義你的功能像
public static String Utils(int something){
Geocoder geoCoder = new Geocoder(context, Locale.getDefault());
List<Address> address = null;
if (geoCoder != null) {
try {
address = geoCoder.getFromLocation(lat, lng, 1);
} catch (IOException e1) {
e1.printStackTrace();
}
if (address.size() > 0) {
return address.get(0).getUtil(something);
}
}
return null; }}
這將被稱為
Utils(Address.GET_COUNTRY_NAME);
Utils(Address.GET_COUNTRY_CODE);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.