如何在其他类中引用变量

huangapple go评论79阅读模式
英文:

How to reference variables in other classes

问题

以下是您提供的代码的中文翻译:

我只是觉得我没有正确地做这个我已经进行了研究但似乎无法理解如何初始化必须引用另一个方法或类的变量

Player 类

public class Player
{
   //实例变量
   private String name;
   private ScoreCard card;
   private int fullDriveYards;
   private int fullDrives;
   private int drivesOnTarget;
}

这是 ScoreCard 类的变量

public class ScoreCard
{
    private int totalStrokes;
    private int versusPar;
    private LinkedList<Integer> playerScoreCard;
}

这是我初始化 Player 类实例变量的方式我没有编写 Scorecard 类因为我不必这样做但我仍然添加了它以便您可以看到发生了什么我只需要知道我是否走在正确的道路上还是完全错了

// Player 类的对象构造函数
public Player(String PlayerName)
{
       // 初始化所有实例变量
       // 查找并处理 playerName 的空引用
       // 为了初始化 this.card,创建 ScoreCard 实例并分配引用
       this.name = PlayerName;
       PlayerName = "UNKNOWN";
       ScoreCard card = this.card;
       this.card = new ScoreCard();
       this.longestDrive = 0;
       this.fullDriveYards = 0;
       this.fullDrives = 0;
       this.drivesOnTarget = 0;
}

如果您需要进一步的帮助或解释,请随时提出。

英文:

I just feel like I'm not doing this correctly and I have researched and just can't seem to wrap my head around how to initialize variable that have to reference another method or class.

Player class

public class Player
{
   //instance variables
   private String name;
   private ScoreCard card;
   private int fullDriveYards;
   private int fullDrives;
   private int drivesOnTarget;
}

Here's the ScoreCard class variables

public class ScoreCard
{
    private int totalStrokes;
    private int versusPar;
    private LinkedList&lt;Integer&gt; playerScoreCard;
}

So this is how I have initialized the instance variables is Player class I did not code the Scorecard class as I didn't have to but I still added it so you can see what's going on. I just need to know if I am on the right track or completely off.

// Constructor for objects of class Player
public Player(String PlayerName)
{
       // initialize all instance variables
       // look for and handle a null reference for playerName
       // to initialize this.card create ScoreCard instance and assign reference
       this.name = PlayerName;
       PlayerName = &quot;UNKNOWN&quot;;
       ScoreCard card = this.card;
       this.card = new ScoreCard();
       this.longestDrive = 0;
       this.fullDriveYards = 0;
       this.fullDrives = 0;
       this.drivesOnTarget = 0;
}

答案1

得分: 0

以下是翻译好的部分:

有一些初始化方面的问题。

  • 没有处理空的玩家名称。
  • 将未初始化的 ScoreCard 分配给本地的 card 变量。虽然不会真正影响任何事情,但这是多余的,当构造函数完成时,card 的值将超出范围。

检查玩家是否为null或具有空白名称(包括为空)。请注意,在尝试检查是否为空之前,必须先检查playerName是否为null。否则,如果playerName为null,可能会出现空指针异常。

对于分数卡,只需将card分配为ScoreCard的实例。假定您的某些玩家方法将根据需要使用字段card来修改分数卡。

// 用于类Player的构造函数
public Player(String playerName)
{
    // 初始化所有实例变量
    // 查找并处理玩家名称的空引用
    // 要初始化this.card,请创建ScoreCard实例并分配引用

    if (playerName == null || playerName.isBlank()) {
        this.name = "UNKNOWN";
    } else {
        this.name = playerName;
    }
    this.card = new ScoreCard();
    this.longestDrive = 0;
    this.fullDriveYards = 0;
    this.fullDrives = 0;
    this.drivesOnTarget = 0;
}
英文:

There are a couple things wrong with the intialization.

  • didn't handle a null player name.
  • Assigned an uninitialized ScoreCard to a local card variable. Didn't really hurt anything but it is redundant and that value of card will go out of scope when the constructor is finished.

Check if the player is null or has a blank name (including being empty). Note you must check if playerName is null first before attempting to check if it is blank. Otherwise, you may get a null pointer exception if the playerName is null.

For the score card, just assign the card an instance of ScoreCard. It is presumed some of your player methods will modify the scorecard as needed by simply using the field card.

// Constructor for objects of class Player
public Player(String PlayerName)
{
       // initialize all instance variables
       // look for and handle a null reference for playerName
       // to initialize this.card create ScoreCard instance and assign reference
      
       if (playerName == null || playerName.isBlank()) {
            this.name = &quot;UNKNOWN&quot;;
       } else {
           this.name = playerName;
       }
       this.card = new ScoreCard();
       this.longestDrive = 0;
       this.fullDriveYards = 0;
       this.fullDrives = 0;
       this.drivesOnTarget = 0;
}

</details>



# 答案2
**得分**: 0

"I just feel like I'm not doing this correctly ..."
"这只是不完全正确,所以你可能得不到你想要的结果。"
In your constructor method for Player, you are assigning PlayerName to the name field, and then subsequently assigning PlayerName to "UNKNOWN".
在Player的构造方法中,你将PlayerName赋给了name字段,然后随后将PlayerName赋给了"UNKNOWN"。
Assigning PlayerName will have no effect on name.
将PlayerName分配给name将不会对name产生任何影响。
```java
this.name = PlayerName;
PlayerName = "UNKNOWN";

In your comment you mention,
在你的注释中提到,

// look for and handle a null reference for playerName

You'll need a conditional statement for this.
你需要一个条件语句来处理这个问题。

if (PlayerName == null) PlayerName = "UNKNOWN";
this.name = PlayerName;

Furthermore, you are assigning your class variable card to a new local variable card.
此外,你将类变量card分配给了一个新的局部变量card。
This, again, will have no effect, since this.card would be null at this point.
同样,这将不会产生任何效果,因为此时this.card将为null。

// to initialize this.card create ScoreCard instance and assign reference

The following will suffice.
以下内容足够了。

this.card = new ScoreCard();

So, the complete constructor should look like the following.
因此,完整的构造函数应该如下所示。

public Player(String PlayerName)
{
    // initialize all instance variables
    // look for and handle a null reference for playerName
    // to initialize this.card create ScoreCard instance and assign reference
    if (PlayerName == null) PlayerName = "UNKNOWN";
    this.name = PlayerName;
    this.card = new ScoreCard();
    this.longestDrive = 0;
    this.fullDriveYards = 0;
    this.fullDrives = 0;
    this.drivesOnTarget = 0;
}

"... I have researched and just can't seem to wrap my head around how to initialize variable that have to reference another method or class. ..."
"……我已经做了研究,但似乎无法理解如何初始化必须引用另一个方法或类的变量。"
Your assignment is correct,
你的赋值是正确的,

this.card = new ScoreCard();

Since your ScoreCard class variables are declared as private, you won't be able to access them via dot-notation—e.g., card.totalStrokes.
由于你的ScoreCard类变量声明为私有,所以你无法通过点表示法访问它们,例如card.totalStrokes。
You will need to either remove the modifier, change the modifier to public, or create methods that will provide the access.
你需要删除修饰符,将修饰符更改为public,或创建提供访问权限的方法。

public class ScoreCard
{
    private int totalStrokes;
    private int versusPar;
    private LinkedList<Integer> playerScoreCard;

    public int getTotalStrokes() {
        return totalStrokes;
    }

    public void setTotalStrokes(int totalStrokes) {
        this.totalStrokes = totalStrokes;
    }

    public int getVersusPar() {
        return versusPar;
    }

    public void setVersusPar(int versusPar) {
        this.versusPar = versusPar;
    }

    public LinkedList<Integer> getPlayerScoreCard() {
        return playerScoreCard;
    }

    public void setPlayerScoreCard(LinkedList<Integer> playerScoreCard) {
        this.playerScoreCard = playerScoreCard;
    }
}

Then, you can access the values as follows.
然后,你可以按以下方式访问这些值。

card.setTotalStrokes(123);
card.setVersusPar(123);
card.setPlayerScoreCard(new LinkedList<>());

Here are some references by Java which may help in your coding process.
以下是一些Java的参考资料,可能有助于你的编码过程。
Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects).
Lesson: Object-Oriented Programming Concepts (The Java™ Tutorials > Learning the Java Language).

英文:

> "I just feel like I'm not doing this correctly ..."

It's correct, there are just some inconsistencies, so you may not get the result you're looking for.

In your constructor method for Player, you are assigning PlayerName to the name field, and then subsequently assigning PlayerName to "UNKNOWN".

Assigning PlayerName will have no effect on name.

this.name = PlayerName;
PlayerName = &quot;UNKNOWN&quot;;

In your comment you mention,

// look for and handle a null reference for playerName

You'll need a conditional statement for this.

if (PlayerName == null) PlayerName = &quot;UNKNOWN&quot;;
this.name = PlayerName;

Furthermore, you are assigning your class variable card to a new local variable card.

This, again, will have no effect, since this.card would be null at this point.

// to initialize this.card create ScoreCard instance and assign reference

The following will suffice.

this.card = new ScoreCard();

So, the complete constructor should look like the following.

public Player(String PlayerName)
{
    // initialize all instance variables
    // look for and handle a null reference for playerName
    // to initialize this.card create ScoreCard instance and assign reference
    if (PlayerName == null) PlayerName = &quot;UNKNOWN&quot;;
    this.name = PlayerName;
    this.card = new ScoreCard();
    this.longestDrive = 0;
    this.fullDriveYards = 0;
    this.fullDrives = 0;
    this.drivesOnTarget = 0;
}

> "... I have researched and just can't seem to wrap my head around how to initialize variable that have to reference another method or class. ..."

You're assignment is correct,

this.card = new ScoreCard();

Since you're ScoreCard class variables are declared as private, you won't be able to access them via dot-notation&mdash;e.g., card.totalStrokes.

You will need to either remove the modifier, change the modifier to public, or create methods that will provide the access.

public class ScoreCard
{
    private int totalStrokes;
    private int versusPar;
    private LinkedList&lt;Integer&gt; playerScoreCard;

    public int getTotalStrokes() {
        return totalStrokes;
    }

    public void setTotalStrokes(int totalStrokes) {
        this.totalStrokes = totalStrokes;
    }

    public int getVersusPar() {
        return versusPar;
    }

    public void setVersusPar(int versusPar) {
        this.versusPar = versusPar;
    }

    public LinkedList&lt;Integer&gt; getPlayerScoreCard() {
        return playerScoreCard;
    }

    public void setPlayerScoreCard(LinkedList&lt;Integer&gt; playerScoreCard) {
        this.playerScoreCard = playerScoreCard;
    }
}

Then, you can access the values as follows.

card.setTotalStrokes(123);
card.setVersusPar(123);
card.setPlayerScoreCard(new LinkedList&lt;&gt;());

Here are some references by Java which may help in your coding process.

Classes (The Java&trade; Tutorials &gt; Learning the Java Language &gt; Classes and Objects).
Lesson: Object-Oriented Programming Concepts (The Java&trade; Tutorials &gt; Learning the Java Language).

huangapple
  • 本文由 发表于 2023年6月29日 10:33:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577731.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定