Object Construction

Calling Another Constructor

If the first statement of a constructor has the form this(. . .), then the constructor calls another constructor of the same class. Here is a typical example:

public Employee(double s){   // calls Employee(String, double)   this("Employee #" + nextId, s);   nextId++;}

 

When you call new Employee(60000), then the Employee(double) constructor calls the Employee(String, double) constructor.

Using the this keyword in this manner is useful—you only need to write common construction code once.

 

 

java.util.Random 1.0

 

  • Random()

    constructs a new random number generator.

  • int nextInt(int n) 1.2

    returns a random number between 0 and n - 1.

posted @ 2010-09-13 13:11  露初晞  Views(169)  Comments(0)    收藏  举报