为Java类创建了一个测试仪
我创建了一个student类和一个tester,它将为“student”创建3个实例,我将给出“student”和“StudentTester”的两组代码 以及我在尝试编译“StudentTester”时遇到的编译错误 Student.java-为Java类创建了一个测试仪,java,oop,Java,Oop,我创建了一个student类和一个tester,它将为“student”创建3个实例,我将给出“student”和“StudentTester”的两组代码 以及我在尝试编译“StudentTester”时遇到的编译错误 Student.java- import java.io.*; import java.util.*; public class Student { public static void main(String[] args) { Student student = n
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");
}
}
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$
实际上,您在学生类中错过了默认构造函数
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”。(接球手和接球手)
构造函数需要一个参数。所以
studoo=新学生(“Stewie”)
阅读您的代码——您没有任何名为Student.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();
}
}