java类测试器问题

时间:2019-03-18 22:52:08

标签: java oop

我创建了一个学生类,我需要为其创建测试器文件,该文件可以编译,但是当我“ java Student”时出现错误。 这个错误是:

TheRealFawcett:Lab8 therealfawcett$ java Student
Error: Main method not found in class Student, 
please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend 
javafx.application.Application
TheRealFawcett:Lab8 therealfawcett$ 

有人知道为什么会这样吗?以及修复它所要做的事情。

下面的代码是我原来的“学生”课程文件

这是我的代码:

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

public class Student {
    private static void main(String[] args) {
        Student student = new Student("Charles");
    }

    private String forName;
    private String surName;
    private String studentID;
    private String degreeScheme;

    //This is the Constructor of the 
    public Student(String name) {
        this.forName = forName;
    }

    //Assign the surname of the student 
    public void stuSurname(String stuSurname) {
        surName = stuSurname;
    }

    //Assign the student ID to the student
    public void stuID(String stuID) {
        studentID = stuID;
    }

    //Assign the Degree of the Student
    public void stuDegree(String stuDegree) {
        degreeScheme = stuDegree;
    }

    //Print the student details
    public void printStudent() {
        System.out.println("Forname:" + forName);
        System.out.println("Surename:" + 
surName);
        System.out.println("Student ID:" + 
studentID);
        System.out.println("Degree Scheme:" + 
degreeScheme);
    }

    // setter
    public void setForName(String forName) {
        this.forName = forName;
    }

    // getter
    public String getForName() {
        return forName;
    }
}

我还希望在我的原始代码上运行此测试文件,编写的程序是否适合测试?

import java.io.*;

public class StudentTest {

public static void main(String[] args) {
    /*create three new objects using constructor*/
    Student stuOne = new Student()
    Student stuTwo = new Student()
    Student stuThree = new Student()

    //Invoking Methods for Each object Created
    stuOne.forName("James")
    stuOne.stuSurname("Smith")
    stuOne.stuID("0987")
    stuOne.stuDegree("Computer Science")

    stuTwo.forName("Vanessa")
    stuTwo.stuSurname("Peach")
    stuTwo.stuID("0988")
    stuTwo.stuDegree("Mathematics")

    stuThree.forName("George")
    stuThree.stuSurname("BlackSmith")
    stuThree.stuID("0989")
    stuThree.stuDegree("English")
   }
}

测试人员能够创建三个Student实例并将其打印出来。

1 个答案:

答案 0 :(得分:0)

9c53751e中有很多错误(例如,没有以分号结尾的错误),但是特定的OP的问题是关于StudentTest类的。

main类的Student定义为:

main

由于错误状态,签名必须是 public ,因此更改为:

private static void main(String[] args) {

将解决该特定问题。但是,其他地方还有其他问题,例如构造函数:

public static void main(String[] args) {

由于参数为 //This is the Constructor of the public Student(String name) { this.forName = forName; } ,因此必须为:

name

(或更改参数名称,可以选择一个)。