주소록을 디자인하고 있는데 내 AddressBookApp 클래스가 작동하도록하기 위해 (내 주 방법 포함) 인스턴스 변수를 만들어 각각에 대해 정적으로 만들어야했습니다. 메서드를 호출하여 내 이름, 전자 메일 및 전화 개체에 액세스 할 수 있습니다. 나는 더 나은 방법이 있다고 가정하지만 그것이 무엇인지 알기 위해 고심하고있다. 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);
}
}
디자인에 대한 일반적인 의견을 찾고 계시다면 http://codereview.stackexchange.com에 문의하십시오. –
"인스턴스 변수를 만들고 정적으로 만들어야했습니다."- 음, 뭐라구? 인스턴스 변수는 정의에 따라 정적이 아닙니다. 특정 인스턴스와 연결되어 있습니다. –
또한, 용어의 문제로, '정적'은 '인스턴스'변수의 반대입니다. – SJuan76