我正在使用netbeans,我的代码如下
public class GradeBook
{
private String courseName; // course name for this GradeBook
private String courseInstructor; // instructor name for this GradeBook
// constructor initializes courseName and courseInstructor with String Argument
public GradeBook( String name, String insname ) // constructor name is class name
{
courseName = name; // initializes courseName
courseInstructor = insname; // initializes courseInstructor
} // end constructor
// method to set the course name
public void setCourseName( String name )
{
courseName = name; // store the course name
} // end method setCourse
// method to retrieve the course name
public String getCourseName()
{
return courseName;
} // end method getCourseName
// method to set the Instructor name
public void setInstructorName( String insname)
{
courseInstructor = insname; // store the Instructor name
} // end method setInstructorName
// method to retrieve the Instructor name
public String getInstructorName()
{
return courseInstructor;
} // end method getInstructorName
// display a welcome message to the GradeBook user
public void displayMessage()
{
// this statement calls getCourseName to get the
// name of the course this GradeBook represents
System.out.println( "\nWelcome to the grade book for: \n"+
getCourseName()+"\nThis course is presented by: "+getInstructorName());
System.out.println( "\nProgrammed by Jack Friedman");
} // end method displayMessage
} // end
答案 0 :(得分:1)
您应该在main方法中调用此类的构造函数。
按如下方式创建一个新类GradeBookTest:
public class GradeBookTest {
public static void main (String args[]) {
GradeBook book = new GradeBook("Math", "T.I.");
book.displayMessage(); //To see your results
}
}
现在您可以启动此课程来查看结果。
答案 1 :(得分:0)
请为您的应用添加静态主方法。
答案 2 :(得分:0)
主要方法在哪里?包含以下代码以运行您的程序 -
public static void main(String[] args) {
GradeBook book = new GradeBook("subj1", "instruc1");
book.displayMessage();
}
答案 3 :(得分:0)
在Java编程语言中,每个应用程序都必须包含一个签名为:
的main方法public static void main(String[] args)
main
方法是java程序的入口点,必须将其声明为public
,以便可以从类外部static
访问它,以便它可以即使不创建类的实例或对象也可以访问。
为了更好地理解,请阅读this