Java 获取错误:无法从静态上下文引用非静态方法getTotalPlayers()
我看不出我做错了什么,也看不出这里的任何东西是静态的,没有这样声明。我只需要在这里指出正确的方向 测试代码:Java 获取错误:无法从静态上下文引用非静态方法getTotalPlayers(),java,Java,我看不出我做错了什么,也看不出这里的任何东西是静态的,没有这样声明。我只需要在这里指出正确的方向 测试代码: public class PaintballPlayerTest { //Test program for PaintballPlayer assignment public static void main (String [] args) { //Part 1 check constructor & toString --(make sure ID is working t
public class PaintballPlayerTest
{
//Test program for PaintballPlayer assignment
public static void main (String [] args)
{
//Part 1 check constructor & toString --(make sure ID is working too)
PaintballPlayer sheldon = new PaintballPlayer ("Sheldon", "Lee", "Cooper");
PaintballPlayer leonard = new PaintballPlayer ("Leonard", "Hofstadter");
PaintballPlayer amy = new PaintballPlayer ("Amy", "Farrah", "Fowler");
System.out.println(sheldon);
System.out.println(leonard);
//Part 2 test getTotalPlayer --should be 3
System.out.println("The team has this many players " + PaintballPlayer.getTotalPlayers());
我的代码:
import java.util.*;
public class PaintballPlayer
{
private String firstName, middleName, lastName;
private String secFirst, secLast;
private int id;
private int count;
private static int totalPlayers;
private int playerID;
private int players;
public PaintballPlayer(String first, String middle, String last)
{
count = 0;
id = totalPlayers++;
players = count++;
firstName = first;
middleName = middle;
lastName = last;
}
public PaintballPlayer(String f, String l)
{
this (f,"",l);
id = count++;
}
public PaintballPlayer()
{
totalPlayers++;
}
public static int getTotalPlayers()
{
return totalPlayers;
}
public String toString()
{
String name;
String n;
name = firstName + " " + middleName + " " + lastName;
return name;
}
public int getPlayerID()
{
playerID = count;
return playerID;
}
}
同样,我的问题是getTotalPlayers()方法
编辑:这是应用提供的修复程序编辑的代码。谢谢大家! getTotalPlayers()
不是一个静态方法,因此需要一个PaintballPlayer
的实例来调用此方法
如果要在PaintballPlayer
中存储玩家总数,则需要一个静态属性(所有实例的引用相同):
getTotalPlayers()
不是一个静态方法,因此需要使用PaintballPlayer
的实例来调用此方法
如果要在PaintballPlayer
中存储玩家总数,则需要一个静态属性(所有实例的引用相同):
在您的代码中,构造函数#1存在一些问题 变量player和count将被重置,而变量count将增加两倍 在构造函数2中,问题最严重:
public PaintballPlayer(String f, String l)
{
this (f,"",l);
id = count++;
players = count++;
}
因为除了原始构造函数中的2倍之外,还要将count的值增加2倍,所以总共增加了4倍
一旦你完成了德文·弗里斯所说的,你就必须修改PaintballPlayer的构造器
public PaintballPlayer(String first, String middle, String last)
{
id = totalPlayers++;
firstName = first;
middleName = middle;
lastName = last;
}
在您的代码中,构造函数#1存在一些问题 变量player和count将被重置,而变量count将增加两倍 在构造函数2中,问题最严重:
public PaintballPlayer(String f, String l)
{
this (f,"",l);
id = count++;
players = count++;
}
因为除了原始构造函数中的2倍之外,还要将count的值增加2倍,所以总共增加了4倍
一旦你完成了德文·弗里斯所说的,你就必须修改PaintballPlayer的构造器
public PaintballPlayer(String first, String middle, String last)
{
id = totalPlayers++;
firstName = first;
middleName = middle;
lastName = last;
}
试试这个
import java.util.*;
public class PaintballPlayer {
private String firstName, middleName, lastName;
private String secFirst, secLast;
private int id;
private static int count;
private static int totalPlayers;
private int playerID;
private static int players;
public PaintballPlayer(String first, String middle, String last) {
count++;
id = count;
players = count;
firstName = first;
middleName = middle;
lastName = last;
}
public PaintballPlayer(String f, String l) {
this(f, "", l);
id = count;
players = count;
}
public String toString() {
String name = firstName + " " + middleName + " " + lastName;
return name;
}
public static int getTotalPlayers() {
totalPlayers = players;
return totalPlayers;
}
public int getPlayerID() {
playerID = count;
return playerID;
}
}
class PaintballPlayerTest {
//Test program for PaintballPlayer assignment
public static void main(String[] args) {
//Part 1 check constructor & toString --(make sure ID is working too)
PaintballPlayer sheldon = new PaintballPlayer("Sheldon", "Lee", "Cooper");
PaintballPlayer leonard = new PaintballPlayer("Leonard", "Hofstadter");
PaintballPlayer amy = new PaintballPlayer("Amy", "Farrah", "Fowler");
System.out.println(sheldon);
System.out.println(leonard);
//Part 2 test getTotalPlayer --should be 3
System.out.println("The team has this many players " + PaintballPlayer.getTotalPlayers());
}
}
试试这个
import java.util.*;
public class PaintballPlayer {
private String firstName, middleName, lastName;
private String secFirst, secLast;
private int id;
private static int count;
private static int totalPlayers;
private int playerID;
private static int players;
public PaintballPlayer(String first, String middle, String last) {
count++;
id = count;
players = count;
firstName = first;
middleName = middle;
lastName = last;
}
public PaintballPlayer(String f, String l) {
this(f, "", l);
id = count;
players = count;
}
public String toString() {
String name = firstName + " " + middleName + " " + lastName;
return name;
}
public static int getTotalPlayers() {
totalPlayers = players;
return totalPlayers;
}
public int getPlayerID() {
playerID = count;
return playerID;
}
}
class PaintballPlayerTest {
//Test program for PaintballPlayer assignment
public static void main(String[] args) {
//Part 1 check constructor & toString --(make sure ID is working too)
PaintballPlayer sheldon = new PaintballPlayer("Sheldon", "Lee", "Cooper");
PaintballPlayer leonard = new PaintballPlayer("Leonard", "Hofstadter");
PaintballPlayer amy = new PaintballPlayer("Amy", "Farrah", "Fowler");
System.out.println(sheldon);
System.out.println(leonard);
//Part 2 test getTotalPlayer --should be 3
System.out.println("The team has this many players " + PaintballPlayer.getTotalPlayers());
}
}
好的,太好了!这很有效。但是我知道有0名球员,总共应该有3名。现在playerID运行时是2,现在是4。嗯。看你帖子上的评论。你把
totalPlayers
属性改为static
?我改了。通过删除“playerID”变量,我修复了playerID部分。我正在试图弄清楚为什么它仍然返回0名玩家。@GnomezGrave:我应用了你的建议,将我的ID降回0,而不是保留在2。我的球员总数仍然是0。谢谢你的帮助,太好了!这很有效。但是我知道有0名球员,总共应该有3名。现在playerID运行时是2,现在是4。嗯。看你帖子上的评论。你把totalPlayers
属性改为static
?我改了。通过删除“playerID”变量,我修复了playerID部分。我正在试图弄清楚为什么它仍然返回0名玩家。@GnomezGrave:我应用了你的建议,将我的ID降回0,而不是保留在2。我的球员总数仍然是0。感谢您的帮助。代码中存在语义问题。为什么要在构造函数上设置count=0
。和id=count++代码>和玩家=count++代码>,这些将从2开始递增计数。我建议您使用count++代码>然后id=count代码>玩家=计数代码>且不使<代码>计数=0代码>代码中存在语义问题。为什么要在构造函数上设置count=0
。和id=count++代码>和玩家=count++代码>,这些将从2开始递增计数。我建议您使用count++代码>然后id=count代码>玩家=计数代码>且不使<代码>计数=0代码>修改后我总共有4个玩家,玩家id现在是2。所以playerID=good和totalPlayers现在只需要3岁。我已经编辑了这篇文章。看起来还没有被修改。我还在使用旧代码,几分钟后我会查看帖子。现在可以了。应用此修复程序时,我必须更改一点。谢谢修改后我总共有4个玩家,玩家id现在是2。所以playerID=good和totalPlayers现在只需要3岁。我已经编辑了这篇文章。看起来还没有被修改。我还在使用旧代码,几分钟后我会查看帖子。现在可以了。应用此修复程序时,我必须更改一点。谢谢很抱歉将此作为新答案发布。但是由于询问者感到困惑,我已经发布了这个。谢谢。你们都帮了我大忙。我很感激。很抱歉将此作为新答案发布。但是由于询问者感到困惑,我已经发布了这个。谢谢。你们都帮了我大忙。我很感激。