Java custom optional values for constructor -
This code is too much for me to do:
import java.util Random; Public class testclass (protected intestine test; Protected Ent TestWeb; Public Test Class () {Random R = New Random (); This (r.nextInt (), r.nextInt ()); } Public Test Class (Int TestVara, Int TestWeb) {this.testVarA = startTestVarA; This.testVarB = startTestVarB; }}
However, it does not compile, because this ()
statement should be next to the function. I can do something like this. (New Random ()) GetNextInt (), (New Random). GetNextInt ())
But it seems very unfair. What is the right way to do this?
You can move complex initial arguments in a different way:
public test class () {random R = new random (); Init (r.nextInt (), r.nextInt ()); } Public Test Class (Int TestVarva, Int TestWeb) {init (Testwada, TestWarb)} Private Zero Init (Int TestVara, Int TestWeb) {this.testVarA = startTestVarA; This.testVarB = startTestVarB; }
This is a more general solution than being a field as random
. In this special case, the random
field works because the class In all the instances of this, it can be shared without side effects, but if you want to do something that is not as thread-safe as a beginner, then it may become a problem about not being eligible for garbage collection. Here in Not to talk about the static field. ever
Comments
Post a Comment