用于重载方法的Java动态转换 [英] Java dynamic casting for overloaded methods

查看:107
本文介绍了用于重载方法的Java动态转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个函数,我可以将名称和值传递到 ContentValues 。我遇到的问题是我试图允许一次传递更多的一个键/值对,因此键可以是一个字符串数组,但值必须是对象,而ContentValues.put()不是允许对象,它们需要被强制转换为String,Double,Float等。有没有办法可以确定对象的类型并将其强制转换,以便调用正确的put()?下面是一个有效的方法,但它要求我为每种可能的值添加一个if。

I'm attempting to create a function where I can pass names and values that will be put into a ContentValues. The problem i'm having is that I'm trying to allow passing more that one key/value pair at a time, so keys can be an array of strings, but values have to be objects and ContentValues.put() doesn't allow Objects, they need to be cast to String, Double, Float, etc. Is there a way I can determine the type of the object and cast it so it will call the correct put()? Below is one method that does work, but it requires me to add an if for every possible type of value.

 public long create(String[] names, Object[] values) {
        ContentValues initialValues = new ContentValues();

        for (int i = 0; i < names.length; i++)
        {
            String type = values[i].getClass().getName();
            if (type.equals("java.lang.Double"))
            {
                initialValues.put(names[i], (Double)values[i]);
            }
            else if (type.equals("java.lang.String")) {
                initialValues.put(names[i], (String)values[i]);
            }
            else
            {
                throw new InvalidParameterException("Unable to convert type:"+type);
            }
        }

        return mDb.insert(this.getTableName(), null, initialValues);
    }


推荐答案

你的方法是正确的,尽管你应该使用 instanceof 而不是 getClass()。getName()。 ie:

Your approach is the right one, though you should use instanceof instead of getClass().getName(). ie:

if (values[i] == null) {
  initialValues.putNull(names[i]);
} else if (values[i] instanceof Boolean) {
  initialValues.put(names[i], (Boolean)values[i]);
} else if (values[i] instanceof Byte) {
  initialValues.put(names[i], (Byte)values[i]);
} else if (values[i] instanceof Double) {
  initialValues.put(names[i], (Double)values[i]);
} else if (values[i] instanceof Float) {
  initialValues.put(names[i], (Float)values[i]);
} else if (values[i] instanceof Integer) {
  initialValues.put(names[i], (Integer)values[i]);
} else if (values[i] instanceof Long) {
  initialValues.put(names[i], (Long)values[i]);
} else if (values[i] instanceof Short) {
  initialValues.put(names[i], (Short)values[i]);
} else if (values[i] instanceof String) {
  initialValues.put(names[i], (String)values[i]);
} else if (values[i] instanceof byte[]) {
  initialValues.put(names[i], (byte[])values[i]);
} else if (values[i] instanceof ContentValues) {
  initialValues.putAll(names[i], (ContentValues)values[i]);
} else {
  throw new IllegalArgumentException(
      "can't put " + values[i].getClass().getName() + " in ContentValues.");
}

另一种选择是使用反射,但我认为反射是一种方法不得已。

Another option would be to use reflection, but I consider reflection to be a method of last resort.

如果 ContentValues 不是 final 另一种选择是扩展它并添加一个采用 Object 的方法。

If ContentValues wasn't final another option would be to extend it and add a method that takes an Object.

这篇关于用于重载方法的Java动态转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆