public class Testing{
private String firstName;
private String lastName;
private double salary;
private String subject;
private String highestDegree;
private int years;
public Testing
(String first, String last, String sub, String degree, double sal, int year)
//constructor being called in the main method.
{
lastName = last;
firstName = first;
subject = sub;
highestDegree = degree;
salary = sal;
years = year;
}
public class Hello{
public static void main(String []args){
//Part missing
}
}
我做了setter和getter,我所缺少的是如何在main方法中调用该构造函数。我正在考虑创建一个新的对象,例如Testing in = new Testing(),但我很确定我错过了其他的东西。如果还有更多我可能遗失的事情,请告诉我。我正在学习java。
答案 0 :(得分:2)
由于您已定义了自定义构造函数
public Testing (String first, String last, String sub, String degree, double sal, int year)
你不能使用默认的构造函数。
Testing in = new Testing() // Not allowed now
您必须使用构造函数来定义,实例化和初始化类Testing
的对象。
public static void main(String []args){
String first = "userName";
...
...
Testing in = new Testing(first, last, sub, degree, sal, year)
}