java泛型方法的覆盖,当参数从泛型函数被覆盖时,将泛型参数clazz放到RealmQuery中...

实际上我正在用Kotlin为Android Studio项目编写一个存储库模式 . 我有一个名为“BaseRepository”的通用接口 . 此接口从“BaseRealmRepository”和其他接口扩展而来 .

我需要将Realm.db的所有结果都放到findAll()中,但是我无法修改这个继承的函数,因为它在其他存储库中是通用的 .

当我尝试时,我收到以下错误:

"fun findAllRealm ( clazz: Class ) : List is not satisfied: inferred type GenericRealmModelType<> is not a subtype of RealmModel"*

但我真正的问题是 clazz parameter of Class , I need do cast or something 传递给"where"的realmQuery without modify the generic function adding upper bounds like or similar

这是我的通用界面:

interface BaseRepository {

fun create(item: T) : T

fun remove(item: T)

fun update(item: T) : T

fun find(item: T): T

fun findAll(clazz: Class): List

}

我为do cast创建了一个泛型类型:

interface GenericRealmModelType{}

最后,我从“BaseRepository”扩展的抽象类“BaseObjectsRealmRepository”:

abstract class BaseObjectsRealmRepository : BaseRepository {

...

override fun findAll(clazz: Class): List {

var klazz: Class> = clazz as Class>

val allResults = findAllRealm(klazz)

return allResults

}

private fun findAllRealm(clazz: Class): List{

val realm = Realm.getDefaultInstance()

val realmResults = realm

.where(clazz) //CLAZZ IS MY PROBLEM

.findAll()

val allResults: List = realm.copyFromRealm(realmResults)

return allResults

}

}

项目配置如下:

项目build.gradle:

buildscript {

ext.kotlin_version = '1.2.41'

repositories {

google()

jcenter()

}

dependencies {

classpath 'com.android.tools.build:gradle:3.1.2'

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

classpath "io.realm:realm-gradle-plugin:5.1.0"

classpath 'com.google.gms:google-services:3.2.0'

// NOTE: Do not place your application dependencies here; they belong

// in the individual module build.gradle files

}

}

allprojects {

repositories {

google()

jcenter()

maven {

url "https://maven.google.com"

}

}

}

task clean(type: Delete) {

delete rootProject.buildDir

}

模块应用build.gradle:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'

apply plugin: 'realm-android'

android {

compileSdkVersion 27

defaultConfig {

applicationId "com.example.example"

minSdkVersion 16

targetSdkVersion 27

versionCode 1

versionName "1.0"

testInstrumentationRunner

"android.support.test.runner.AndroidJUnitRunner"

}

buildTypes {

release {

minifyEnabled false

proguardFiles getDefaultProguardFile('proguard-android.txt'),

'proguard-rules.pro'

}

}

}

dependencies {

implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

implementation 'com.android.support:appcompat-v7:27.1.1'

implementation 'com.android.support.constraint:constraint-layout:1.1.0'

testImplementation 'junit:junit:4.12'

androidTestImplementation 'com.android.support.test:runner:1.0.2'

androidTestImplementation 'com.android.support.test.espresso:espresso-

core:3.0.2'

implementation 'com.google.dagger:dagger-android:2.12'

implementation 'com.google.dagger:dagger-android-support:2.12'

kapt 'com.google.dagger:dagger-android-processor:2.12'

kapt 'com.google.dagger:dagger-compiler:2.13'

implementation 'com.android.support:support-annotations:27.1.1'

implementation 'com.google.firebase:firebase-auth:15.1.0'

implementation 'com.google.android.gms:play-services-auth:15.0.1'

implementation 'com.google.firebase:firebase-core:15.0.2'

implementation 'com.android.support:support-v4:27.1.1'

implementation 'com.android.support:recyclerview-v7:27.1.1'

implementation 'com.android.support:design:27.1.1'

}

realm {

syncEnabled = true

}

repositories {

mavenCentral()

}

// Add to the bottom of the file

apply plugin: 'com.google.gms.google-services'

我能做什么?

谢谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值