为Java类创建了测试器

时间:2019-03-19 04:11:53

标签: java oop

我创建了一个学生班级和一个测试人员,它将为“ student”创建3个实例,我将提供“ Student”和“ StudentTester”的两组代码。

以及我尝试编译“ StudentTester”时遇到的编译错误。

Student.java-

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

public class Student {
public 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 forName) {
        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;
    }
}

StudentTester.java-

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.java可以正常编译并运行,但是测试人员在编译时会发出这些错误,有人可以帮助我我为什么得到这些错误吗?

TheRealFawcett:lab8 therealfawcett$ javac 
StudentTest.java
StudentTest.java:7: error: constructor Student in 
class Student cannot be applied to given types;
        Student stuOne = new Student();
                     ^
  required: String
  found: no arguments
  reason: actual and formal argument lists differ 
in length
StudentTest.java:8: error: constructor Student in 
class Student cannot be applied to given types;
        Student stuTwo = new Student();
                     ^
  required: String
  found: no arguments
  reason: actual and formal argument lists differ 
in length
StudentTest.java:9: error: constructor Student in 
class Student cannot be applied to given types;
        Student stuThree = new Student();
                       ^
  required: String
  found: no arguments
  reason: actual and formal argument lists differ 
in length
StudentTest.java:12: error: cannot find symbol
        stuOne.forName("James");
          ^
  symbol:   method forName(String)
  location: variable stuOne of type Student
 StudentTest.java:17: error: cannot find symbol
        stuTwo.forName("Vanessa");
          ^
  symbol:   method forName(String)
  location: variable stuTwo of type Student
StudentTest.java:22: error: cannot find symbol
        stuThree.forName("George");
            ^
  symbol:   method forName(String)
  location: variable stuThree of type Student
6 errors
TheRealFawcett:lab8 therealfawcett$ 

1 个答案:

答案 0 :(得分:0)

实际上,您错过了Student类中的默认构造函数。

package com.date.example;
import java.io.*;
import java.util.*;

public class Student {
public 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 forName) {
        this.forName = forName;
    }

    public Student() {
        // TODO Auto-generated constructor stub
    }

    //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;
    }
}

尝试使用“ setForName”而不是“ forName”。 (字母和二传手)

package com.date.example;
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.setForName("James");
    stuOne.stuSurname("Smith");
    stuOne.stuID("0987");
    stuOne.stuDegree("Computer Science");

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

    stuThree.setForName("George");
    stuThree.stuID("0989");
    stuThree.stuDegree("English");
//Invoking the printStudentmethod.
    stuOne.printStudent();
    System.out.println("\n");
    stuTwo.printStudent();
    System.out.println("\n");
    stuThree.printStudent();

   }
 }