[英]How to call a created class in a test class in java?
I'm new to Java.我是 Java 的新手。 I created a public class called Student and I have also created a test class for it.我创建了一个名为 Student 的公共 class 并且我还为它创建了一个测试 class。 The thing is I don't know how to call my created class "Student", inside of the Test class.问题是我不知道如何在测试 class 内部将我创建的 class 称为“学生”。 I have placed both classes in the same folder, yet the Test class is giving out errors saying.我已将这两个类放在同一个文件夹中,但测试 class 给出了错误提示。
TestStudent.java
setName cannot be resolved or is not a field
setPhoneNum cannot be resolved or is not a field
setCgpa cannot be resolved or is not a field
setSubject cannot be resolved or is not a field
setAddress cannot be resolved or is not a field
Here is my code for Student class:这是我的学生 class 的代码:
public class Student {
private String name;
private int phoneNum;
private double cgpa;
private String subject;
private String address;
//getters
public String getName(){
return name;
}
public int getPhoneNum(){
return phoneNum;
}
public double getCgpa(){
return cgpa;
}
public String getSubject(){
return subject;
}
public String getAddress(){
return address;
}
//setters
public void setName (String newName){
this.name = newName;
}
public void setPhoneNum (int newPhoneNum){
this.phoneNum = newPhoneNum;
}
public void setCgpa (double newCgpa){
this.cgpa = newCgpa;
}
public void setSubject (String newSubject){
this.subject = newSubject;
}
public void setAddress (String newAddress){
this.address = newAddress;
}
}
Here is my code for TestStudent class:这是我为 TestStudent class 编写的代码:
public class TestStudent {
public static void main(String[] args){
Student Alex = new Student();
Alex.setName = "Alexis";
Alex.setPhoneNum = 193;
Alex.setCgpa = 4.0;
Alex.setSubject = "English, Biology";
Alex.setAddress = "Rainbow Land";
System.out.println(Alex.getName());
}
}
Could anyone guide me on this?有人可以指导我吗?
I think your TestStudent
class should look like this:我认为您的TestStudent
class 应该如下所示:
public class TestStudent {
public static void main(String[] args){
Student Alex = new Student();
Alex.setName("Alexis");
Alex.setPhoneNum(193);
Alex.setCgpa(4.0);
Alex.setSubject("English, Biology");
Alex.setAddress("Rainbow Land");
System.out.println(Alex.getName());
}
}
You create an Student
object and then call the setters with the argument in the brackets.您创建一个Student
object,然后使用括号中的参数调用设置器。 There is no assigning with =
at all.根本没有用=
分配。 This is done inside of the setter methods.这是在 setter 方法内部完成的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.