尝试创建一个对象的多个实例并打印其信息

时间:2019-06-20 17:17:37

标签: java oop object

尝试创建一个具有NFL球队统计数据的程序。 “团队”类创建对象,并且main方法将打印统计信息。当我尝试打印统计信息时,所有团队只会打印最后一个团队的属性。因此,当我尝试打印Arizona时,它会打印Buffalo。代码如下:

//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);

    }
}

3 个答案:

答案 0 :(得分:1)

static表示“该类的所有实例将共享相同的变量”。这意味着当构造函数被调用时,您每次都在更改值。因此,只有最后一支队伍坚持下来。

Here是学习staticnon-static的好资源

要解决此问题,您只需删除的静态修饰符

public static int offenseRating;
public static int defenseRating;
public static int kickerRating;
public static String teamname;

,并在构造函数中使用this.而不是Team.

答案 1 :(得分:0)

您必须从字段和构造函数中删除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);
}

此关键字表示将构造方法参数的值写入当前实例的字段。

答案 2 :(得分:0)

如前所述,静态变量是“类变量”,在声明它们后始终可以访问。它们通常用于声明有关程序的常量(您应检查适合您需要的Enum结构)。类的字段是“实例变量”,它们的目的是保持在内存中创建的特定数据结构的特定状态。出于安全原因,保护那些内存空间很重要。

不重现此错误的一种很好的方法是将注意力集中在数据的用例上。先是特定对象(类的实例)的特定变量,然后是实例变量。

下面是说明这一点的代码,并且可以编译

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());
    }
}