java - 设计问题: Static vs Non-static instance variables

标签 java static instance-variables

我正在设计一个地址簿,为了使我的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);
}
}

最佳答案

AddressBookAddressBookApp 应该是两个不同的类。 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 传递给您需要它的任何方法,或者继续创建新实例。您还可以将其作为应用程序类中的静态引用:

private static AddressBook book = new AddressBook();

// in your app class methods
book.burnBeforeReading();

关于java - 设计问题: Static vs Non-static instance variables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14165092/

相关文章:

c - 初始化静态多维空终止数组

ruby - 访问父类实例变量

java - 在java中将文件导入到二维数组

c# - 使 Console.WriteLine() 包装器与额外的格式化参数一起工作,例如 "{0}"

java - 在 fragment 链之间传递数据的标准方法

java - 无法从 Activity 类访问 GLSurfaceview 类中声明的变量(没有静态)

java - 在 Java 中如何使用变量?

java - Java中的实例变量是什么?

java - 实现依赖注入(inject)困惑

java - 使用自定义类名反序列化具有不同根名称的 json