1

我以前没有使用过泛型。现在是我应该创建一个泛型类的时候了,否则我的代码长度会变得更大更难理解。

这是将记录插入到位于 Windows Azure 的数据库的方式:

public class Item {
    public int Id;
    public String Text;
}

在定义移动客户端的同一活动中,添加以下代码:

Item item = new Item();
item.Text = "Awesome item";
mClient.getTable(Item.class).insert(
        item, 
        new TableOperationCallback<Item>() { 
            public void onCompleted(
                    Item entity,
                    Exception exception, 
                    ServiceFilterResponse response
            ) { 
                if (exception == null) { 
                     // Insert succeeded 
                } else { 
                    // Insert failed 
                } 
            } 
        });

我无法创建通用类来对位于 Windows Azure 的数据库进行操作,例如插入、删除等。

这是 Windows azure 参考的链接(如果需要):

http://dl.windowsazure.com/androiddocs/

我尝试了以下代码:

public class WindowsAzureOperations<T> {

    T mT;
    MobileServiceClient mClient;
    Context mContext;

    public void insert(MobileServiceClient mClient, T tObject, final Context tmpContext) {

        this.mClient = mClient;
        mT = tObject;
        mContext = tmpContext;

        this.mClient.getTable(mT.getClass()).insert(mT,  // error in this line
                new TableOperationCallback<T>() {

                    public void onCompleted(T entity, Exception exception,
                            ServiceFilterResponse response) {

                        if (exception == null) {

                        } else {

                        }
                    }
                });
    }
}

它显示以下错误:

insert(capture#1-of ? extends Object, TableOperationCallback<capture#1-of ? extends Object>)类型中的方法MobileServiceTable<capture#1-of ? extends Object>不适用于参数(T, new TableOperationCallback<T>(){})

请帮我。提前致谢。

4

1 回答 1

2

问题在于你如何打电话getTable。给定 a Class<T>,该方法将返回 a MobileServiceTable<T>。但mT.getClass()返回 a Class<? extends T>,因为运行时类型mT可能是T 或某种扩展类型 T

作为一种解决方法,让您的insert方法采用类型完全相同的参数Class<T>

public void insert(
        MobileServiceClient mClient,
        T tObject,
        Class<T> objectType,
        final Context tmpContext
) {

    this.mClient = mClient;
    mT = tObject;
    mContext = tmpContext;

    this.mClient.getTable(objectType).insert(
            mT,
            new TableOperationCallback<T>() {

                public void onCompleted(T entity, Exception exception,
                        ServiceFilterResponse response) {

                    if (exception == null) {

                    } else {

                    }
                }
            });
}

mClient我还建议您将、mT和的赋值mContext移至WindowsAzureOperations. 事实上,从表面上看你的类,没有必要实例化它 - 只需使用静态泛型方法:

public final class WindowsAzureOperations {

    // now a utility class so prevent instantiation
    private WindowsAzureOperations() { }

    public static <T> void insert(
            MobileServiceClient mClient,
            T tObject,
            Class<T> objectType,
            final Context tmpContext
    ) {
        mClient.getTable(objectType).insert(
                tObject,
                new TableOperationCallback<T>() {
                    @Override
                    public void onCompleted(
                            T entity,
                            Exception exception,
                            ServiceFilterResponse response
                    ) {
                        if (exception == null) {
                            //...
                        }
                        else {
                            //...
                        }
                    }
                }
        );
    }
}
于 2013-07-22T14:13:47.660 回答