简体   繁体   English

跟踪实例化对象数量的Java类变量

[英]Java class variable that tracks number of objects instantiated

I have this class, Student, with the variable StudentID: 我有一个带有变量StudentID的班级Student:

public class Student extends Person{
  int studentID = 0;
  int level;

  public Student(){

  }

  public Student(String fName, String lName, int gLevel){
    super(fName, lName);
    if(gLevel >= 0 && gLevel <= 12){
      level = gLevel;
    }
    studentID++;
  }
  public int getLevel(){
    return level;
  }
  public String toString(){
    String toReturn;
    toReturn = super.toString() + "\n   Grade Level: " + level + "\n   ID #: " + studentID;
    return toReturn;
  }
}

I want the variable StudentID to keep assign each Student created a new ID number. 我希望变量StudentID继续分配给每个Student创建的新ID号。 Each ID number should be one more than the last ID number created, and so equal to the total number of objects that have been created. 每个ID号都应比上一个创建的ID号大一个,并且应等于已创建的对象总数。 Right now each object is assigned the ID number of 1. 现在,每个对象的ID号为1。

Add a static counter and initialize studentID with it, incrementing it in the process : 添加一个静态counter并用它初始化studentID ,并在此过程中增加它:

public class Student extends Person{
  static counter = 1;
  int studentID = counter++;
  ...

You need a static variable to keep track of the number of students objects created. 您需要一个静态变量来跟踪创建的学生对象的数量。

public class Student extends Person{

    /* Number of students objects created */
    private static int studentCount = 0;   
    int studentID = 0;
    int level;

    public Student(String fName, String lName, int gLevel){
        super(fName, lName);
        if(gLevel >= 0 && gLevel <= 12){
            level = gLevel;
        }
        studentID = Student.getNextStudentId();
    }

    private static synchronized int getNextStudentId() {
        /* Increment the student count and return the value */
        return ++studentCount;
    }
}

Make the studentID a static member 将studentID设为静态成员

Static members are kept throughout each instance of the class no matter how many instances of the clas there are. 静态成员将在整个类的每个实例中保留,无论有多少个clas实例。

   public class Student extends Person{
      static int studentID = 0;
      int level;

      public Student(){

      }

      public Student(String fName, String lName, int gLevel){
        super(fName, lName);
        if(gLevel >= 0 && gLevel <= 12){
          level = gLevel;
        }
        studentID++;
      }
      public int getLevel(){
        return level;
      }
      public String toString(){
        String toReturn;
        toReturn = super.toString() + "\n   Grade Level: " + level + "\n   ID #: " + studentID;
        return toReturn;
      }
    }

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

相关问题 在没有实例化对象的情况下,如何在 Java 中建立 class 类型的类型? - How to establish the type of a class type in Java, without instantiated objects? 如何更新从 Java 中的同一类实例化的实例化对象中的变量? - How do I update variables in instantiated objects that are instantiated from the same class in Java? Java类变量号 - Java class variable number 如何判断哪个类被实例化并通过Java中的一个变量来访问它 - How to determine which class is instantiated and access it through a variable in Java 在Java的另一个类中实例化/初始化的对象的引用变量 - Reference variable to an object instantiated/initialized in another class in Java 对实例化对象执行Java反射 - Perform java reflection on instantiated objects 为什么Java并发处理不能与新实例化的对象一起工作,而与同一类的反序列化对象一起工作呢? - Why does Java concurrent processing not work with newly instantiated objects, while it works with deserialized objects of the same class? Java:实例化对象变量错误 - Java: Instantiated object variable error Java:如何在另一个类中使用在一个类中实例化的对象? - Java: How can I use objects instantiated in one class in another class? 获取Java中的实例化类型变量 - get instantiated type variable in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM