我有两种类似的方法,但它们的工作方式有所不同。注意:getBytesDownloaded()、getFileSize() 返回 long。
此方法返回的整数值正是我所期望的(例如:51)
public int getPercentComplete() throws IOException
{
int complete = (int) Math.round(this.getBytesDownloaded()*100 / this.getFileSize());
return complete;
}
但是此方法在运行时不返回任何值(即使我将 int 更改为 long),尽管它编译正常:
public int getCurrentSpeed() throws IOException
{
long KBytesDownloaded = this.getBytesDownloaded() / 1024;
currentTime = System.currentTimeMillis();
int speed = (int) Math.round(KBytesDownloaded * 1000 / (currentTime - startTime));
return speed;
}
错误:
Exception in thread "Timer-0" java.lang.NoSuchMethodError: com.myclasses.Downloa
d.getCurrentSpeed()F
at test$2.run(test.java:87)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)
为了解决这个问题,我将int改为float,它工作正常(例如:300.0)
public float getCurrentSpeed() throws IOException
{
long KBytesDownloaded = this.getBytesDownloaded() / 1024;
currentTime = System.currentTimeMillis();
float speed = KBytesDownloaded * 1000 / (currentTime - startTime));
return speed;
}
为什么两个相似的方法不返回相同的类型值?谢谢你。