使用静态方法java的类中的私有构造函数

时间:2018-02-05 01:21:57

标签: java arraylist static-methods private-constructor

我正在尝试创建一个按标记降序对数组列表进行排序的类。由于我的所有方法都是静态的,我想编写一个构造函数来防止类实例化,但我不确定如何去做。我读到可以使用私有构造函数但不确定如何编写它。

这是我的代码:

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

public class ProcessDegreeMark{

public static ArrayList<Finalist> finalistsInList(String s) throws Exception{
    ArrayList<Finalist> finalists = new ArrayList<Finalist>();
    String id;
    double mark;
    Scanner in = null;
    try
    {
        in = new Scanner(new FileReader(s));
        try
        {
            while(in.hasNextLine())
            {
                id =in.nextLine();
                mark = Double.parseDouble(in.nextLine());
                finalists.add(new Finalist(id,mark));
            }
        }
        finally
        {
            in.close();
        }
    }
    catch(IOException e)
    {
        System.out.println(s+" not found");
    }
    return finalists;
 }


public static void displayFinalists(ArrayList<Finalist> finalists){
  for (int i = 0; i < finalists.size(); i++)
  {
       System.out.println(finalists.get(i));
  }
}


public static void findFinalistID(ArrayList<Finalist> a, String s){
    int count =0;
    for (int i=1;i<a.size();i++)
    {
        if (((a.get(i))).getId().equals(s))
        {
            System.out.println(a.get(i));
            count++;
        }
    }
    if(count==0)
    {
        System.out.println("No candidate found with ID number "+s);
    }
}

public static void findFinalistClass(ArrayList<Finalist> a, String s){
    int count =0;
    for (int i=1;i<a.size();i++)
    {
        if (((a.get(i))).getdegreeClass().equals(s))
        {
            System.out.println(a.get(i));
            count++;
        }
    }
    if(count==0)
    {
        System.out.println("No candidate found with degree class "+s);
    }
}

public static ArrayList<Finalist> sortDegreeMark(ArrayList<Finalist> a){
    ArrayList<Finalist> sortedFinalists = new ArrayList<Finalist>();
    sortedFinalists.addAll(a);
    Collections.sort(sortedFinalists, new FinalistComparator());
    return sortedFinalists;


}

public static void finalistsToFile2(ArrayList<Finalist> finalists, String s) {
    PrintStream out = null;
    try
    {
        out = new PrintStream(new FileOutputStream(s));
        try
        {
            for(int i = 0; i < finalists.size(); i++)
            {
                out.println(finalists.get(i));
            }
        }
        finally
        {
            out.close();
        }
    }
    catch (FileNotFoundException e)
    {
        e.printStackTrace();
    }

}

public static void findAndSaveFinalistClass(ArrayList<Finalist> a, String s){
    ArrayList<Finalist> searchFinalists = new ArrayList<Finalist>();
    int count =0;
    for (int i=1;i<a.size();i++)
    {
        if (((a.get(i))).getdegreeClass().equals(s))
        {
            System.out.println(a.get(i));
            searchFinalists.add(a.get(i));
            finalistsToFile2(searchFinalists,"testSorted.txt");
            count++;
        }
    }
    if(count==0)
    {
        System.out.println("No candidate found with degree class "+s);
    }
}

    public static void main(String[] args) throws Exception{
 System.out.println("/****************************************************/");
         System.out.println("/*******finalistsInList with invalid file name*******/");
         System.out.println();
         ArrayList<Finalist> testList = finalistsInList("file***.txt");
         System.out.println();
         System.out.println("/****************************************************/");
         System.out.println("/********finalistsInList with valid file name********/");
         System.out.println("/********display to check arraylist populated********/");
         System.out.println();
         ArrayList<Finalist> finalists = finalistsInList("finalMark.txt");
         displayFinalists(finalists);
         System.out.println();
         System.out.println("/****************************************************/");
         System.out.println("/*testing findFinalistID with valid and invalid data*/");
         System.out.println();
         findFinalistID(finalists, "75021");  
         findFinalistID(finalists, "21050");
         System.out.println();
         System.out.println("/****************************************************/");
         System.out.println("/*test findFinalistClass with valid and invalid data*/");
         System.out.println();
         findFinalistClass(finalists, "FIRST"); 
         findFinalistClass(finalists, "THIRD"); 
         System.out.println();
         System.out.println("/****************************************************/");
         System.out.println("/*****run sortedFinalists then test with display*****/");
         System.out.println();
         ArrayList<Finalist> sortedFinalists = sortDegreeMark(finalists);
         displayFinalists(sortedFinalists);
         System.out.println();
         System.out.println("/****************************************************/");
         System.out.println("/*****test finalistsToFile2 with sorted arraylist*****/");
         System.out.println("/**************check file testSorted.txt**************/");
         System.out.println();
         finalistsToFile2(sortedFinalists, "testSorted.txt"); //save the sorted arraylist to a new file, check by opening file
         System.out.println();
         System.out.println("/****************************************************/");
         System.out.println("/*test findAndSaveFinalistClass with valid and invalid data*/");
         System.out.println();
         findAndSaveFinalistClass(finalists, "FIRST"); //test method finds
         findAndSaveFinalistClass(finalists, "THRID"); //check appropriate error message when nothing found, open new text file
         System.out.println();
         System.out.println("/*********************THE END************************/");
     }
}

2 个答案:

答案 0 :(得分:2)

静态方法属于该类。我真的不明白你为什么担心没有课程实例化。无论是创建一个实例还是多个实例,调用者都将拥有相同的静态方法副本。尽管如此,您仍然可以通过添加如下所示的默认私有构造函数来限制类外的即时性

private ProcessDegreeMark(){}

答案 1 :(得分:1)

只需在构造函数之前添加private修饰符。

public class ProcessDegreeMark{
    private ProcessDegreeMark(){}
}