I am creating a gwt application.I get 开发者_如何学JAVAthe error "The constructor Random() is not visible" in the method below.I have tried putting it in the main onModuleLoad() as well.
public void jump(){
Random generator = new Random();
}
I have imported random via the following
import com.google.gwt.user.client.Random;
No matter where I put this I still keep getting that error.
Thanks
Don't instantiate it. Instead, call the static methods on the class:
Random.nextInt(...)
The com.google.gwt.user.client.Random class has no public constructor, and all of its methods are static. The javadoc says:
This class can be used as a substitute for java.util.Random. The semantics differ in that the underlying browser's implementation is used. The random generator cannot be seeded or otherwise used to reproduce a particular sequence of results.
I suspect that the reason for this design (i.e. static methods and no public constructor) is that the GWT Random class is really just a skin for the ECMAScript function Math.random(), which is a static function and which cannot be seeded.
I guess using java.util.Random is an alternative, but I would not trust it to be able to seed itself from a browser-side source of entropy.
加载中,请稍侯......
精彩评论