我试图在java中使用getattr
方法来模仿python的Method func = exampleObject.getClass().getMethod("methodName", Object[].class);
func.invoke(exampleObject, args);
方法,以找到一个与输入字符串同名的方法" methodName",但是我收到错误说
无法将方法解析为类型
以下是代码:
public class LocalUtil extends Application {
private static Context context1;
private static DisplayMetrics metrics;
public static void setContext(Context context)
{
context1=context;
metrics=context1.getResources().getDisplayMetrics();
}
public static float getDensity()
{
return metrics.density;
}
public static int getScreenWidth()
{
return metrics.widthPixels;
}
public static int getScreenHeight()
{
return metrics.heightPixels;
}
public static float getScreenWidthInDpi()
{
return metrics.widthPixels/metrics.density;
}
public static float getScreenHeightInDpi()
{
return metrics.heightPixels/metrics.density;
}
答案 0 :(得分:3)
您似乎忘记了“方法”的导入?
import java.lang.reflect.Method;