简体   繁体   English

如何从方法中声明一个私有的 static final int(java 初学者)

[英]How to declare a private static final int from a method (java beginner)

First things first... I am new at java and have only very recently begun experimenting with multi-class and multi-method code.首先,我是 Java 新手,最近才开始尝试多类和多方法代码。

So, I have a program that I have to write that takes the grades of students in a class (read from an external file) and then calculate the final grade for each student.因此,我必须编写一个程序,该程序获取班级学生的成绩(从外部文件中读取),然后计算每个学生的最终成绩。 I am currently writing the bit of code that determines how many students are in the class.我目前正在编写确定班级中有多少学生的代码。 This involves counting the number of lines in the external file.这涉及计算外部文件中的行数。

This is what I have so far...这是我到目前为止...

 import java.io.*;
 import java.util.Scanner;

 public class Student
 {
 // instance variables 
private int Quiz1;
private int Quiz2;
private int MidTerm;
private int Final;
private int FinalP;
private int LetterGrade;


public Student(int q1, int q2, int mt, int f)
{
   Quiz1 = q1;
   Quiz2 = q2;
   MidTerm = mt;
   Final = f;
}

public int getStudentNumber()
{
   Scanner two = null;
    try 
   {
      // Create a scanner to read the file, file name is parameter
       two = new Scanner (new File("Prog349f.txt"));
      } 
    catch (FileNotFoundException e) 
    {
    System.out.println ("File not found!");
    // Stop program if no file found
    System.exit (0);
    } 

   int lines = 0;   
    while(two.hasNext()) 
    {    
    lines++;
    two.nextLine();
   }
   two.close();
   return(lines);  
 }  
}

I have decided that I want to save the number of lines/number of students as a private static final int, (the current classification is just a placeholder).我决定将行数/学生人数保存为私有静态最终整数,(当前分类只是一个占位符)。 I am just not quite sure how to save it as such.我只是不太确定如何保存它。 In all of the examples, private static final(s) are declared like this:在所有示例中,私有静态 final(s) 声明如下:

 private static final double PI = 3.14; 

tldr; tldr; I am more than sure that this has been covered many times already, but what is the protocol for defining a public static final using a method?我非常确定这已经被多次覆盖,但是使用方法定义公共静态最终的协议是什么?

You have misunderstood what static final is for, it's only for cases where a value can be set at the time that the class is loaded, and never changes after that.您误解了 static final 的用途,它仅适用于可以在加载类时设置值并且此后永远不会更改的情况。 It's useful for declaring constants (like PI in your example) where you know what it should hold before the program ever runs.这对于声明常量(如您的示例中的 PI)很有用,您知道在程序运行之前它应该保存什么。

Instead, for each line you read in, create a Student and add it to a list.相反,对于您读入的每一行,创建一个学生并将其添加到列表中。 Whenever you want the number of students you can call the size method on the list.每当您想要学生人数时,您都可以调用列表上的 size 方法。

If you are really attached to the idea of making something static final, you could declare the List that way.如果您真的热衷于制作静态最终版的想法,您可以以这种方式声明 List。 The reference that the static final variable contains never changes but you can add to and modify its contents.静态最终变量包含的引用永远不会改变,但您可以添加和修改其内容。

Cramming the file-reading code into the Student class as an instance method seems confused, you should find a better place for it.将读取文件的代码作为实例方法塞进 Student 类似乎很混乱,您应该为它找到一个更好的地方。 Reading all of the students from a file is not logic that belongs to an individual Student.从文件中读取所有学生不是属于单个学生的逻辑。 Java isn't class-oriented or method-oriented, it's object-oriented. Java 不是面向类或面向方法的,它是面向对象的。 Put code in methods so they will be relevant to the instance you call them on.将代码放在方法中,以便它们与您调用它们的实例相关。

My observation/s :我的观察:

1) Each line in the file "Prog349f.txt" represents the grades of each student. 1)“Prog349f.txt”文件中的每一行代表每个学生的成绩。 So N lines mean N student in the class.所以 N 行表示班上的 N 个学生。

2) You want to store this number of student in a private static final variable. 2)您想将此学生人数存储在私有静态最终变量中。

This what you can do :这是你可以做的:

1) You can define getStudentNumber() as static (logic : the associated file is a class level file that contains grades of all students in the class and because the file is class level, the method reasonably can be declared as static). 1) 您可以将 getStudentNumber() 定义为静态(逻辑:关联文件是一个包含班级所有学生成绩的班级文件,并且由于该文件是班级的,因此该方法可以合理地声明为静态)。

public static int getStudentNumber() {
int lines = 0;
try(Scanner two = new Scanner(new File("clients")))  {
    while(two.hasNext()) {
        lines++;
        two.nextLine();
    }
} catch (FileNotFoundException e) {
        System.out.println ("File not found!");
        System.exit(0);
}
    return(lines);
}

2) And then call getStudentNumber() in the main class or any other class where you wish to save this number as private static final variable. 2)然后在主类或您希望将此数字保存为私有静态最终变量的任何其他类中调用 getStudentNumber() 。

public class Main {

private static final int NUMBER_OF_STUDENTS = Student.getStudentNumber();

public static void main(String[] args){

    System.out.println(NUMBER_OF_STUDENTS);

    }
}

3) However, the implementation efficiency of your case study can be improved a lot. 3)但是,您的案例研究的实施效率可以提高很多。 The way you have implemented this may not be the most perfect one.您实现这一点的方式可能不是最完美的。 This will require careful analysis of the case.这需要仔细分析案件。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM