简体   繁体   中英

Questions about Java constructor, for class and its super class

I'm trying to figure out how the constructors work in the following program. Basically there is a Seeker class that extends Player class, and a Game class that contains Seeker object, as shown in the code below.

My question is, when the Game class instantiates mySeeker using mySeeker = new Seeker( "Sally", this ); does this invoke the player's constructor? If so, where did it pass on the name of the game? I saw a Game g in the constructor for Player , but I can't figure out how the game myGame (in the main method) has been passed to the Seeker .

//Game class

class Game {

   Seeker mySeeker;
   int gridSize;

   Game( int gs ) {
     gridSize = gs;
     mySeeker = new Seeker( "Sally", this ); //what does 'this' do here? why it does not include the name of the game?
   }

   void play() {
     mySeeker.seek();
   }
}

//Player class

  abstract class Player { 

     private Point location;
     private String name;
     Game game;

Player( Point p, String n, Game g ) {
    location = p; 
    name = n;
    game = g;
}

//Seeker class

class Seeker extends Player {

   Seeker( String n, Game g ) {
       super( new Point( 0, 0 ), n, g ); // seeker starts at 0,0
}

//Main Program

public static void main(String[] args) {

    int gridSize = 3; // default value
    Game myGame = new Game( gridSize );
    myGame.play();
}

The "this" reference is referring to the instance of the Game class. The game class is lending itself to the seeker/player class so the seeker/player class can access data from the game class.

edit: The "seeking" is being handed "this" instance of "Game" and using the "super" command to pass data to it's inherited "player"

My question is, when the Game class instantiate mySeeker using mySeeker = new Seeker( "Sally", this ); does ``this invoke the player's constructor?

No.

The Player constructor >>is<< being invoked, but not because of this . In the context where that statements comes from, this will denote a Game instance. It is "just a parameter" that is being passed to the Seeker constructor.

The Player constructor is being invoked in the Seeker` constructor; specifically this line:

    super( new Point( 0, 0 ), n, g );

What happens is that when you new the Seeker , the Seeker constructor is called, and it immediately calls the Player constructor, via the super call.


If so, where did it pass on the name of the game?

The line above is where the Game object is being passed to Player . See the g parameter? It the same Game object that you passed as this .

(The code you have shown us doesn't support a "name" for the game. What is being passed is a reference. And when you assign the new Game instance to myGame , it isn't the "name of the game". The myGame variable is simply that ... a variable ... that can contain a reference to some Game object.)

Seeker gets instantiated with a name and a Game object. Passing this is just a reference to the current object. The Seeker will be created with the already created Game object meaning the Seeker can access data from the Game object.

mySeeker = new Seeker( "Sally", this )

The above code allocate a Seeker object, and then execute its constructor to initiate its value, and then assign the variable mySeeker with a reference on the new Seeker object.

The constructor is called with two arguments: a string and this . this refers to the current object that is executing the above code, in this case it refers to the Game object that is being constructed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM