java - 为 Java 类创建了一个测试器
问题描述
我创建了一个学生类和一个测试器,它将为“student”创建 3 个实例,我将为“Student”和“StudentTester”提供两组代码。
以及我尝试编译“StudentTester”时遇到的编译错误。
学生.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$
解决方案
实际上,您错过了 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();
}
}
推荐阅读
- javascript - 使用javascript更改元素ID?
- ruby - 如何在线程中使用超时限制
- google-bigquery - BigQuery JDBC 驱动程序数组和结构
- json - 遍历字典元素并将它们添加到列表中
- slick.js - 如何在 slick 中设置按钮的样式和位置?
- php - Elastisearch 无法进行 JSON 编码问题
- java - Hybris 中的自定义产品 URL 解析器
- c# - AsParallel 写入同一个对象
- android - Mapbox 如何将地图标签显示为该地点本地使用的语言,以及地图其余部分中的英文地图标签
- c++ - void指针的相关性