我有一个具有两个重载方法的Android-Java类

package com.test;

public class A{

  public void theMethod(Bitmap bitmap){
    ...
  }

  public void theMethod(int resource){
    ...
  }
}


我正在尝试在Nativescript-Angular程序中扩展该类:

class ExtendedA extends com.test.A{
    public theMethod(bitmap : android.graphics.Bitmap) : void{
        ...
    }
}


但是我有这个错误

Property 'theMethod' in type 'ExtendedA' is not assignable to the same property in base type ‘A’.
  Type ‘(bitmap : Bitmap) => void' is not assignable to type '{ (param: Bitmap): void; (param : number): void; }'.
    Types of parameters 'bitmap' and 'param' are incompatible.
      Type 'number' is not assignable to type 'Bitmap'.


P.D.我没有com.test.A类代码。

最佳答案

在tsconfig.json文件中,您需要创建-strictFunctionTypes:true

"compilerOptions": {
    "module": "commonjs",
    "strictFunctionTypes": true,
    "downlevelIteration": true
 }


附言如果您的参数计数在A类中不同,则Java方法重载由开发人员通过显式检查调用函数的参数计数来处理。

10-06 14:03
查看更多