检查通用函数android java中的对象类型

时间:2022-11-02 16:52:57

I have a function in my android application:

我的android应用程序中有一个函数:

public static <T> void saveLocalData(Context context, String key, Class<T> value) {
    // Check type of value here
    SharedPreferences prefs = context.getSharedPreferences(
            Constants.PREFERENCES_KEY, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    if(value.isAssignableFrom(String.class)){
        //Put value here
    }
    else if(value.isAssignableFrom(Boolean.class)){
        //Put value here            
    }

    editor.commit();
}

I want to check type of value in this function (I want to check two type Boolean and String), but I don't know how to do it! Can anyone give any suggestion? Thanks!

我想在这个函数中检查值的类型(我想检查两个类型的Boolean和String),但我不知道该怎么做!任何人都可以提出任何建议吗?谢谢!

Edited: Thanks every for help! I have one more problem that is how to save it value to Preferences?

编辑:谢谢大家的帮助!我还有一个问题是如何将它保存到首选项?

2 个解决方案

#1


4  

You could use Class.isAssignableFrom().

您可以使用Class.isAssignableFrom()。

Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter

确定此Class对象表示的类或接口是否与指定的Class参数表示的类或接口相同,或者是它们的超类或超接口


UPDATE: By the edit in the question, and in order to set the key/value in a SharedPreferences.Editor, use putString() and putBoolean().

更新:通过问题中的编辑,并为了在SharedPreferences.Editor中设置键/值,使用putString()和putBoolean()。

Take into account you'll need to receive the value as an argument. Notice that if you receive the value, you can already access its class (so you don't need Class<T> as an argument nor isAssignableFrom()), and check it by means of the instanceof operator:

考虑到你需要接收价值作为参数。请注意,如果您收到该值,则您已经可以访问其类(因此您不需要Class 作为参数,也不需要isAssignableFrom()),并通过instanceof运算符进行检查:

public static <T> void saveLocalData(Context context, String key, T value) {
    SharedPreferences prefs = context.getSharedPreferences(
        Constants.PREFERENCES_KEY, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    if (value instanceof String) { 
        editor.putString(key, (String) value);
    }
    else if (value instanceof Boolean){
        editor.putBoolean(key, (Boolean) value);
    }
    editor.commit();
}

#2


1  

I haven't tried it on Android, but since it's syntax is usually equivalent to Java, here's two ways to do it in Java:

我没有在Android上尝试过它,但由于它的语法通常与Java相同,所以在Java中有两种方法:

if (Boolean.class.equals(value) || String.class.equals(value)) {
    // Do stuff.
}

This works because Boolean and String are both final and you don't have to worry about extended classes. I think you could even use == in this case (assuming a normal ClassLoader) as typically there's only one instance of Class per Class on the classpath. If you need to worry about extending classes, use:

这是有效的,因为布尔值和字符串都是最终的,您不必担心扩展类。我认为你甚至可以在这种情况下使用==(假设一个普通的ClassLoader),因为类路径上每个类只有一个Class实例。如果您需要担心扩展类,请使用:

if (Boolean.class.isAssignableFrom(value) || String.class.isAssignableFrom(value)) {
    // Do stuff.
}

#1


4  

You could use Class.isAssignableFrom().

您可以使用Class.isAssignableFrom()。

Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter

确定此Class对象表示的类或接口是否与指定的Class参数表示的类或接口相同,或者是它们的超类或超接口


UPDATE: By the edit in the question, and in order to set the key/value in a SharedPreferences.Editor, use putString() and putBoolean().

更新:通过问题中的编辑,并为了在SharedPreferences.Editor中设置键/值,使用putString()和putBoolean()。

Take into account you'll need to receive the value as an argument. Notice that if you receive the value, you can already access its class (so you don't need Class<T> as an argument nor isAssignableFrom()), and check it by means of the instanceof operator:

考虑到你需要接收价值作为参数。请注意,如果您收到该值,则您已经可以访问其类(因此您不需要Class 作为参数,也不需要isAssignableFrom()),并通过instanceof运算符进行检查:

public static <T> void saveLocalData(Context context, String key, T value) {
    SharedPreferences prefs = context.getSharedPreferences(
        Constants.PREFERENCES_KEY, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    if (value instanceof String) { 
        editor.putString(key, (String) value);
    }
    else if (value instanceof Boolean){
        editor.putBoolean(key, (Boolean) value);
    }
    editor.commit();
}

#2


1  

I haven't tried it on Android, but since it's syntax is usually equivalent to Java, here's two ways to do it in Java:

我没有在Android上尝试过它,但由于它的语法通常与Java相同,所以在Java中有两种方法:

if (Boolean.class.equals(value) || String.class.equals(value)) {
    // Do stuff.
}

This works because Boolean and String are both final and you don't have to worry about extended classes. I think you could even use == in this case (assuming a normal ClassLoader) as typically there's only one instance of Class per Class on the classpath. If you need to worry about extending classes, use:

这是有效的,因为布尔值和字符串都是最终的,您不必担心扩展类。我认为你甚至可以在这种情况下使用==(假设一个普通的ClassLoader),因为类路径上每个类只有一个Class实例。如果您需要担心扩展类,请使用:

if (Boolean.class.isAssignableFrom(value) || String.class.isAssignableFrom(value)) {
    // Do stuff.
}