2016-09-26 13 views
0

私はJava How to Program 10版を読んでおり、最初のいくつかの章を辿っています。この例では、SecureRandomクラスを確認する方法を示していますが、完全に私を困惑させる部分があります。なぜオブジェクト参照変数static finalを宣言しますか?

// Fig. 6.8: Craps.java 
// Craps class simulates the dice game craps. 
import java.security.SecureRandom; 

public class Craps 
{ 
    // create secure random number generator for use in method rollDice 
    private static final SecureRandom randomNumbers = new SecureRandom(); 

    // enum type with constants that represent the game status 
    private enum Status { CONTINUE, WON, LOST }; 

    // constants that represent common rolls of the dice 
    private static final int SNAKE_EYES = 2; 
    private static final int TREY = 3; 
    private static final int SEVEN = 7; 
    private static final int YO_LEVEN = 11; 
    private static final int BOX_CARS = 12; 

    // plays one game of craps 
    public static void main(String[] args) 
    { 
     int myPoint = 0; // point if no win or loss on first roll 
     Status gameStatus; // can contain CONTINUE, WON or LOST 

     int sumOfDice = rollDice(); // first roll of the dice 

     // determine game status and point based on first roll 
     switch (sumOfDice) 
     { 
      case SEVEN: // win with 7 on first roll 
      case YO_LEVEN: // win with 11 on first roll 
       gameStatus = Status.WON; 
       break; 
      case SNAKE_EYES: // lose with 2 on first roll 
      case TREY: // lose with 3 on first roll 
      case BOX_CARS: // lose with 12 on first roll 
       gameStatus = Status.LOST; 
       break; 
      default: 
       gameStatus = Status.CONTINUE; // game is not over 
       myPoint = sumOfDice; // remember the point 
       System.out.printf("Point is %d%n", myPoint); 
       break; 
     } 

     // while game is not complete 
     while (gameStatus == Status.CONTINUE) // not WON or LOST 
     { 
      sumOfDice = rollDice(); // roll dice again 

      // determine game status 
      if (sumOfDice == myPoint) // win by making point 
       gameStatus = Status.WON; 
      else if (sumOfDice == SEVEN) // lose by rolling 7 before point 
       gameStatus = Status.LOST; 
     } 

     // display won or lost message 
     if (gameStatus == Status.WON) 
      System.out.println("Player wins"); 
     else 
      System.out.println("Player loses"); 
    } 

    // roll dice, calculate sum and display results 
    public static int rollDice() 
    { 
     // pick random die values 
     int die1 = 1 + randomNumbers.nextInt(6); // first die roll 
     int die2 = 1 + randomNumbers.nextInt(6); // second die roll 

     int sum = die1 + die2; // sum of die values 

     // display results of this roll 
     System.out.printf("Player rolled %d + %d = %d%n", die1, die2, sum); 

     return sum; 
    } 
} // end class Craps 

ブックから、それはその1つのオブジェクトは、常に方法、rollDice()を呼び出すために使用されているように、それはクラスのプライベート静的最終変数、SecureRandomとして宣言されていることを言及しています。クラスCrapsの複数のインスタンスを含むプログラムがある場合、それらはすべてこの1つのオブジェクトを共有します。私の質問は、クラスSecureRandomの複数のインスタンスが必要になるチャンスがあるでしょうか?次の質問は、SecureRandomのオブジェクト参照変数であるため、それはまだCrapsの変数として呼び出されますか?

答えて

1

これはSecureRandomの変数参照されたオブジェクトであるため、次の質問は、なぜCrapsの変数として呼び出されますか?あなたの変数randomNumbers

Crapsクラスのメンバーであり、それはTYPE SecureRandomです。すなわち、タイプSecureRandomのクラップスクラスの変数として使用されることが定義される。静的であると定義されているのは、randomNumbersのコピーが1つだけあり、すべてのインスタンスで共有されます。Craps

関連する問題