简体   繁体   English

有关Java构造函数(用于类及其超类)的问题

[英]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. 基本上,有一个扩展Player类的Seeker类和一个包含Seeker对象的Game类,如下面的代码所示。

My question is, when the Game class instantiates mySeeker using mySeeker = new Seeker( "Sally", this ); 我的问题是,当Game类使用mySeeker = new Seeker( "Sally", this );实例化mySeeker mySeeker = new Seeker( "Sally", this ); does this invoke the player's constructor? this调用玩家的构造函数吗? 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 . 我在Player的构造函数中看到了Game g ,但是我无法弄清楚myGame (在main方法中)如何传递给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. “ this”引用是指Game类的实例。 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 ); 我的问题是,当GamemySeeker using mySeeker = new Seeker(“ Sally”,this)实例化mySeeker using does ``this invoke the player's constructor? does ``this调用玩家的构造函数吗?

No. 没有。

The Player constructor >>is<< being invoked, but not because of this . Player构造函数>> is <<被调用,但不是因为this In the context where that statements comes from, this will denote a Game instance. 在该语句来自的上下文中, this将表示一个Game实例。 It is "just a parameter" that is being passed to the Seeker constructor. 它只是“一个参数”,被传递给Seeker构造函数。

The Player constructor is being invoked in the Seeker` constructor; Player constructor is being invoked in the Seeker`构造函数中Player constructor is being invoked in the函数; 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. 发生的情况是,当您new Seeker ,将调用Seeker构造函数,并通过super调用立即调用Player构造函数。


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 . 上面的行是将Game对象传递给Player See the g parameter? 看到g参数了吗? It the same Game object that you passed as this . this传递的Game对象相同。

(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.) (您显示给我们的代码不支持游戏的“名称”。传递的是引用。当您将新的Game实例分配给myGame ,它不是“游戏的名称”。 myGame变量就是那个变量,可以包含对某些 Game对象的引用。)

Seeker gets instantiated with a name and a Game object. Seeker将使用名称和Game对象实例化。 Passing this is just a reference to the current object. 传递this只是对当前对象的引用。 The Seeker will be created with the already created Game object meaning the Seeker can access data from the Game object. Seeker将与已经创建创建Game对象意味着Seeker可以从访问数据Game对象。

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. 上面的代码分配了一个Seeker对象,然后执行其构造函数以初始化其值,然后为变量mySeeker分配了对新Seeker对象的引用。

The constructor is called with two arguments: a string and this . 用两个参数调用构造函数:字符串和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. 是指正在执行上述代码的当前对象,在这种情况下,它是指正在构造的Game对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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