我正在设计一个地址簿,为了使我的AddressBookApp类工作(包括我的main方法),我不得不创建实例变量并使它们成为静态,以便我的类中的每个方法都能够访问我的名称,电子邮件和电话对象。我认为有更好的方法,但我很难知道那是什么。我应该在main方法中创建对象吗?实例变量是正确的方法吗?你们对我如何改进我的设计有任何想法吗? (如果您有任何其他与我的问题无关的设计建议,请告诉我们)
以下是我的AddressBookApp类的代码:
import java.util.Scanner;
public class AddressBookApp {
//Instance Variables
private static Name name;
private static Email email;
private static Phone phone;
//Constructor
public AddressBookApp() {
name = new Name();
email = new Email();
phone = new Phone();
}
//Main method
public static void main(String[] args) {
new AddressBookApp();
System.out.println("Welcome to the Address Book Application\n");
Scanner sc = new Scanner(System.in);
int menuNumber;
do {
menu();
menuNumber = sc.nextInt();
System.out.println();
if (menuNumber < 1 || menuNumber > 4){
System.out.println("Please enter a valid menu number\n");
} else if (menuNumber == 1) {
printEntries();
} else if (menuNumber == 2) {
addEntry();
} else if (menuNumber == 3) {
removeEntry();
} else {
System.out.println("Thanks! Goodbye.");
sc.close();
return;
}
continue;
} while (menuNumber != 4);
sc.close();
}
/**
* Prints out Main Menu
*/
public static void menu() {
System.out.println("1 - List entries\n" +
"2 - Add entry\n" +
"3 - Remove entry\n" +
"4 - Exit\n");
System.out.print("Enter menu Number: ");
}
/**
* Prints all entries in the Address Book
*/
public static void printEntries() {
name.printNames();
System.out.println();
email.printEmails();
System.out.println();
phone.printPhoneNumbers();
System.out.println();
}
/**
* Adds an entry to the Address Book
*/
public static void addEntry() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Name: ");
name.addName(sc.nextLine());
System.out.print("Enter Email Address: ");
email.addEmail(sc.nextLine());
System.out.print("Enter Phone Number: ");
phone.addPhone(sc.nextLine());
System.out.println("\nRecord Saved.\n");
}
/**
* Removes and entry from the Address Book
*/
public static void removeEntry() {
Scanner sc = new Scanner(System.in);
System.out.print("Please Enter the record number that you would like to remove: ");
int records = sc.nextInt();
name.removeNames(records - 1);
email.removeEmail(records - 1);
phone.removePhone(records - 1);
}
}
答案 0 :(得分:4)
AddressBook
和AddressBookApp
应该是两个不同的类。 AddressBook
应如下所示:
public class AddressBook {
//Instance Variables
private Name name;
private Email email;
private Phone phone;
//Constructor
public AddressBook() {
name = new Name();
email = new Email();
phone = new Phone();
}
// more Constructors
public void setName(Name name) {
this.name = name
}
public Name getName() {
return name;
}
// more getters and setters
然后,您的应用可以使用main()
方法创建此实例:
AddressBook book = new AddressBook();
book.setName(new Name("Jeff"));
//more operations on book
您可以将对象book
传递给您需要的任何方法,也可以继续创建新实例。您还可以将其作为app类中的静态参考:
private static AddressBook book = new AddressBook();
// in your app class methods
book.burnBeforeReading();
答案 1 :(得分:0)
简单,创建两个类:Main
和AddressBook
。
Main只有
public static void main(String[] args) {
new AddressBook().execute();
...
AddressBook
只有实例方法。