每当我尝试编译我的代码时,我都会收到一条错误消息,提示我没有 main 方法。我不确定我需要把它放在哪里或如何放。
import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;
// Abstract Base class for Contacts
abstract class Contact {
private static int contactCount = 0;
private int contactID;
private String firstName;
private String lastName;
private String email;
private String phone;
private String contactType;
public Contact(String firstName, String lastName, String email, String phone, String contactType) {
this.contactID = ++contactCount;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.phone = phone;
this.contactType = contactType;
}
public int getContactID() {
return contactID;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getEmail() {
return email;
}
public String getPhone() {
return phone;
}
public String getContactType() {
return contactType;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setEmail(String email) {
this.email = email;
}
public void setPhone(String phone) {
this.phone = phone;
}
public void setContactType(String contactType) {
this.contactType = contactType;
}
@Override
public String toString() {
return "Contact{" +
"contactID=" + contactID +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", email='" + email + '\'' +
", phone='" + phone + '\'' +
", contactType='" + contactType + '\'' +
'}';
}
}
// Sub-class for Personal Contact
class PersonalContact extends Contact {
private String relationship;
private int age;
private String gender;
public PersonalContact(String firstName, String lastName, String email, String phone, String relationship, int age, String gender) {
super(firstName, lastName, email, phone, "Personal");
this.relationship = relationship;
this.age = age;
this.gender = gender;
}
public String getRelationship() {
return relationship;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
public void setRelationship(String relationship) {
this.relationship = relationship;
}
public void setAge(int age) {
this.age = age;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "PersonalContact{" +
"relationship='" + relationship + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
"} " + super.toString();
}
}
// Sub-class for Family Contact
class FamilyContact extends PersonalContact {
private String relation;
public FamilyContact(String firstName, String lastName, String email, String phone, String relationship, int age, String gender, String relation) {
super(firstName, lastName, email, phone, relationship, age, gender);
this.relation = relation;
super.setContactType("Family");
}
public String getRelation() {
return relation;
}
public void setRelation(String relation) {
this.relation = relation;
}
@Override
public String toString() {
return "FamilyContact{" +"relation='" + relation + '\'';
}
}
我真的不明白为什么会出现错误,因为我认为不需要调用 main 方法。我尝试更改/添加一个主要方法,但我只会收到更多错误。任何帮助将不胜感激!
当我尝试做这样的事情时,我收到一条错误消息说
Error: Main method not found in the file, please define the main method as: public static void main(String[] args)
你确定 Java 不需要 main 方法还是我忘记了一些关于 Java 的东西。已经有一段时间了,如果没有,请告诉我。