开发者

can i @Override an android method to change the parameter types?

开发者 https://www.devze.com 2023-03-08 12:15 出处:网络
this is what the code originally looks like: @Override public void onProgressChanged (SeekBar seekBar, int progress, boolean fromUser){

this is what the code originally looks like:

@Override
public void onProgressChanged (SeekBar seekBar, int progress, boolean fromUser){
    ...
}

i need it to be this:

@Override
public void onProgressChanged (SeekBar seekBar, float progress, boolean fromUser){
    ...
}

however when i change the int to a float i get an error s开发者_如何学运维aying that The method onProgressChanged(SeekBar, float, boolean) of type new SeekBar.OnSeekBarChangeListener(){} must override or implement a supertype method

does anyone know how i can achieve this?


No you can't change types in overriden methods.

If you need float numbers you can pass int number that is multiplied by for example 100 and in your implementation just divide it by 100.

@Override
public void onProgressChanged (SeekBar seekBar, int progress, boolean fromUser){
    float floatProgress = (float)progress/100f;
    ...
}

If not then just cast to int

float progress = 99.0;
onProgressChanged(seekBar, (int)progress, fromUser);


You can't change the type. The compiler understands your method (SeekBar, float, boolean) as a totally different method from (SeekBar, int, boolean).

The error it gives guides you to implement the missing method defined in a base class/interface.

You could (but be careful) change the original interface that your class implements to include a (SeekBar, float, boolean) -method. Or you could just define a different interface altogether.


You cannot do that - when you change the parameter type, you are no long overriding the same method - you are overloading it. You can cast the float to an int before calling onProgressChanged.

0

精彩评论

暂无评论...
验证码 换一张
取 消