简体   繁体   中英

Can I make a Class just has methods?

I want to make a class for File operation like read, write, delete and update This class will have just methods

I have this maindrive

public class HW10 {

    public static void main(String[] args) {

        final String STUDENTS_FILE = "C:\\data\\students.dat";

        RandomAccessFile studentsFile = null ;
        .
        .
        .
        studentsFile = new RandomAccessFile(STUDENTS_FILE, "rw");
        Student student = new Student(1,100,"John","Smith","CIS",3.1) ;
        .
        .
        // write student object to studentsFile
        writeData( studentsFile, student );
        .
        .
        // read from studentsFile
        Student readRecord = new Student() ; 
        studentsFile.seek(0); 
        readData( studentsFile, readRecord );

}

    // read date methods
    private static void readData( RandomAccessFile studentsFile, Student readRecord) 
        { 
         ....   read code
        }
    // write data methods
        private static void writeData( RandomAccessFile studentsFile, Student anItem) 
        { 
        ... write code
        }

How to convert readData and writeData methods to another class like DataAccessFile, So DataAccessFile has all file operation like that

public class DataAccessFile {
    public void writeData(RandomAccessFile studentsFile,Student record){
     .......
    }
  public void readData(RandomAccessFile studentsFile,Student record){
     .....
    }

  public void updateData(RandomAccessFile studentsFile,Student record){
......
}
public void deleteData(RandomAccessFile studentsFile,Student record){
}

}

The Student Class

public class Student {
    // class attributes
    private int    recordStatus ;
    private int studentID ;
    private String lastName ;
    private String firstName ;
    private String majorCode ;
    private double gpa ;
    public Student(int recordStatus, int studentID, String firstName, String lastName ,String majorCode, double gpa) {
        setRecordStatus(recordStatus) ;
        setStudentID(studentID);
        setLastName(lastName);
        setFirstName(firstName);
        setMajorCode(majorCode);
        setGpa(gpa);
    }

    public Student() {
        setRecordStatus(0) ;
        setStudentID(0);
        setLastName("");
        setFirstName("");
        setMajorCode("");
        setGpa(0);
    }
    // getter
    .
    .
    // setter
    .
    .

}

Yes, you can do this; such a class is normally called an utility class . Your DataAccessFile class is a good start, but perhaps FileUtils would be a more fitting name. Here are some general principles for utility classes:

  • Make the class final ; there shouldn't be a reason to create subclasses from a utility class.
  • Make the class non-instantiable by declaring the constructor private .
  • Make all methods static . Client code should use the utility by referencing the class name, eg: FileUtils.writeData(...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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