尝试创建一个以NFL球队统计数据为特色的程序。"Team"类创建对象,main 方法将打印统计信息。当我尝试打印统计数据时,所有团队只会打印上一支团队的属性。所以当我尝试打印亚利桑那州时,它打印的是布法罗。这是代码:
//Team class that creates the Team Objects
public class Team {
public static int offenseRating;
public static int defenseRating;
public static int kickerRating;
public static String teamname;
public Team(String teamname, int offenseRating, int defenseRating,
int kickerRating) {
Team.teamname = teamname;
Team.offenseRating = offenseRating;
Team.defenseRating = defenseRating;
Team.kickerRating = kickerRating;
}
static Team Arizona = new Team("Arizona Cardinals", 50, 50, 50);
static Team Atlanta = new Team("Atlanta Falcons", 50, 50, 50);
static Team Baltimore = new Team("Baltimore Ravens", 50, 50, 50);
static Team Buffalo = new Team("Buffalo Bills", 50, 50, 50);
public class footballMain {
//Main method
public static void main(String[] args) {
System.out.print(Team.Arizona.teamname);
}
}
static
表示"该类的所有实例将共享同一个变量"。这意味着当调用构造函数时,每次都会更改值。因此,只有最后一个团队仍然存在。
这是学习static
与non-static
的好资源
要解决此问题,您应该删除
public static int offenseRating;
public static int defenseRating;
public static int kickerRating;
public static String teamname;
并在构造函数中使用this.
而不是Team.
您必须从字段和构造函数中删除 static 关键字。在这里试试这个:
//Team class that creates the Team Objects
public class Team {
public int offenseRating;
public int defenseRating;
public int kickerRating;
public String teamname;
public Team(String teamname, int offenseRating, int defenseRating,
int kickerRating) {
this.teamname = teamname;
this.offenseRating = offenseRating;
this.defenseRating = defenseRating;
this.kickerRating = kickerRating;
}
static Team Arizona = new Team("Arizona Cardinals", 50, 50, 50);
static Team Atlanta = new Team("Atlanta Falcons", 50, 50, 50);
static Team Baltimore = new Team("Baltimore Ravens", 50, 50, 50);
static Team Buffalo = new Team("Buffalo Bills", 50, 50, 50);
//Main method
public static void main(String[] args) {
System.out.print(Team.Arizona.teamname);
}
this 关键字表示将构造函数参数的值写入当前实例的字段。
正如所指出的,静态变量是"类变量",在声明它们时始终可以访问。它们通常用于声明有关程序的常量(您应该检查可能适合您需求的枚举结构(。类的字段是"实例变量",它们的目的是保留在内存中创建的特定数据结构的特定状态。出于安全原因,保护这些内存空间非常重要。
不重现此错误的一个很好的方法是专注于数据的用例 --> 永远一样然后是静态的;特定对象(类的实例(的特定变量,然后是实例变量。
这是一个代码来说明这一点,并且可以编译
package Question_5669067;
import java.util.Objects;
public class Team
{
// We ensure that the static variable Buffalo will never change.
public final static Team Buffalo = new Team("Buffalo Bills");
// We protect the instance class from calling code.
private String teamName;
private int offenseRating;
public Team(String teamName){
this.teamName = Objects.requireNonNull(teamName, "Null teamName");
}
// We ensure that the String name is returned.
public String getTeamName() {
return this.teamName;
}
public int getOffenseRating(){
return offenseRating;
}
// We ensure that the rate could not get under 0.
public void setOffenseRating(int offenseRating){
if (offenseRating < 0) {
throw new IllegalArgumentException("rate < 0");
}
this.offenseRating = offenseRating;
}
public static void main(String[] args) {
System.out.println(Team.Buffalo.getTeamName());
// Whe are still within the class,
// so we can reach the team name directly.
System.out.println(Buffalo.teamName);
Buffalo.setOffenseRating(13);
System.out.println(Buffalo.getOffenseRating());
}
}