子类无法访问受保护的方法

时间:2020-01-27 13:45:37

标签: android kotlin protected access-modifiers class-visibility

我知道这已经被问过了,但是我的问题很奇怪,我找不到答案。

我有一个父类,其中一个成员被声明为受保护的

class BaseContoller<G: BaseContract.BasePresenter, F: BaseModel> {
    protected var presenter: G? = null
    // ...
}

BaseController的每个子孙中,我毫无疑问地引用presenter,因为它可以在相同的包和子类中访问。

一个BaseController的后代也必须被其他类扩展,但是在再次引用presenter的位置执行某些操作之前,我必须做一个检查

class BackupPrinterController<G: BackupPrinterContract.Presenter, F: BaseModel>: BackupController<G, F>
    private fun startScan() {
        if(this is PrinterAssociationController) {
            PrinterConnection.onConnection = { presenter?.onDeviceConnected(it) }
            // ...
        }
    }
}

这里出现了奇怪的行为,presenter仅在没有过滤器if(this is PrinterAssociationController)的情况下才能完全访问,并带有该if语句,当调用onConnection回调时,一个{{ 1}}由IllegalAccessError

引发

这是PrinterAssociationController的声明

presenter

这是个例外

class PrinterAssociationController: BackupPrinterController<PrinterAssociationContract.Presenter, PrinterAssociationModel>(),
PrinterAssociationContract.View

我不明白为什么在我输入java.lang.IllegalAccessError: Method 'package.common.base.BaseContract$BasePresenter package.common.base.BaseController.getPresenter()' is inaccessible to class 'package.project.features.shared.controllers.BackupPrinterController$startScan$1' (declaration of 'package.project.features.shared.controllers.BackupPrinterController$startScan$1' appears in /data/app/package.project-1/base.apk:classes2.dex) at package.project.features.shared.controllers.BackupPrinterController$startScan$1.invoke(BackupPrinterController.kt:53) at package.project.features.shared.controllers.BackupPrinterController$startScan$1.invoke(BackupPrinterController.kt:35) at package.project.utils.print.PrinterConnection$scan$1$4.accept(PrinterConnection.kt:84) at package.project.utils.print.PrinterConnection$scan$1$4.accept(PrinterConnection.kt:24) at io.reactivex.internal.observers.LambdaObserver.onNext(LambdaObserver.java:63) at io.reactivex.internal.operators.observable.ObservableFilter$FilterObserver.onNext(ObservableFilter.java:52) at io.reactivex.internal.operators.observable.ObservableDistinct$DistinctObserver.onNext(ObservableDistinct.java:85) at io.reactivex.internal.operators.mixed.CompletableAndThenObservable$AndThenObservableObserver.onNext(CompletableAndThenObservable.java:65) at io.reactivex.internal.operators.observable.ObservableFlatMap$MergeObserver.tryEmit(ObservableFlatMap.java:265) at io.reactivex.internal.operators.observable.ObservableFlatMap$InnerObserver.onNext(ObservableFlatMap.java:562) at io.reactivex.internal.operators.observable.ObservableDoOnEach$DoOnEachObserver.onNext(ObservableDoOnEach.java:101) at io.reactivex.internal.operators.observable.ObservableMap$MapObserver.onNext(ObservableMap.java:62) at io.reactivex.internal.operators.observable.ObservableUnsubscribeOn$UnsubscribeObserver.onNext(ObservableUnsubscribeOn.java:60) at io.reactivex.internal.operators.observable.ObservableCreate$CreateEmitter.onNext(ObservableCreate.java:66) at com.polidea.rxandroidble2.internal.serialization.FIFORunnableEntry$1.onNext(FIFORunnableEntry.java:66) at io.reactivex.internal.operators.observable.ObservableUnsubscribeOn$UnsubscribeObserver.onNext(ObservableUnsubscribeOn.java:60) at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeOnObserver.onNext(ObservableSubscribeOn.java:58) at io.reactivex.internal.operators.observable.ObservableCreate$CreateEmitter.onNext(ObservableCreate.java:66) at com.polidea.rxandroidble2.internal.operations.ScanOperationApi21$1.onScanResult(ScanOperationApi21.java:77) at android.bluetooth.le.BluetoothLeScanner$BleScanCallbackWrapper$1.run(BluetoothLeScanner.java:471) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at com.osama.firecrasher.FireLooper.run(FireLooper.kt:39) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6780) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1500) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1390) 语句时会发生这种情况

0 个答案:

没有答案