import java.time.LocalTime
import java.time.temporal.ChronoUnit.SECONDS
class Clocker(initial :Long, increment :Long, interval :Long) {
private val start = LocalTime.now()
def get :Long =
initial + SECONDS.between(start, LocalTime.now()) / interval * increment
}
用法:
// start from 7, increase by 20 every 10 seconds
val clkr = new Clocker(7, 20, 10)
clkr.get //res0: Long = 7
// 11 seconds later
clkr.get //res1: Long = 27
// 19 seconds later
clkr.get //res2: Long = 27
// 34 seconds later
clkr.get //res3: Long = 67
1条答案
按热度按时间yzxexxkh1#
我可以这样做。
用法: