0

如果有更多关于此的帖子,我提前道歉,我已经查看了看起来相关但找不到解决方案的帖子。我在一个名为 Person Tester 的项目中工作;

  1. 提示用户在创建客户 (c) 或员工 (e) 之间进行选择。
  2. 接下来,提示用户输入名字、姓氏、电子邮件地址和客户编号(如果选择了客户)或社会保险号(如果选择了员工)。

  3. 一旦收集到正确的数据,信息就会以这种格式打印到控制台:

        name: first last
        email: jondoe@fakemail.com
        social security number: XXXXXXXXXXX (if employee)
        customer number: XXXXXX  (if customer)
    
  4. 该程序处理继承和一个由员工类和客户类扩展的抽象人员类。另一项规定是,person 类应包含一个名为 getDisplayText 的抽象方法,该方法返回一个字符串。

这是我的问题所在,这是我第一次使用抽象类。

我的问题是为什么我不能简单地在我的 perosn 类中打印 toString 方法来显示输入的所有用户数据,这使我进入下一个问题,程序需要一个额外的组件(我引用我的作业) :要将对象的数据打印到控制台,此应用程序应使用名为 print 的静态方法,该方法接受 Person 对象。

我不确定如何实现这一点,因为它从未在课堂上讨论过。我试图简单地编写以下代码: System.out.print(aPerson.toString) 但我得到的只是名称的空白值:和社会安全号码:。我疯了,我已经为此工作了几个小时,并且至少重新阅读了相关文本 4 次。这是我最后的手段。请引导我朝着正确的方向前进,我不介意长时间工作来做到这一点。

我已经编写了大部分应用程序,现在只是停留在如何将数据打印到控制台上。感谢任何和所有建议,这是我的代码:

public class CH08PR82App {
    public static void main(String[] args) {
        System.out.print("Welcome to the Person Tester Application");
        System.out.println();

        Scanner sc = new Scanner(System.in);

        Person aPerson;
        aPerson = new Person();

        if (aPerson != null) {
            System.out.println(aPerson.toString());
        }

        String choice = "y";
        while (choice.equalsIgnoreCase("y")) {

            //prompt user to enter customer or employee
            System.out.print("Create customer or employee (c/e): ");
            String userType = sc.nextLine();

            if (userType.equalsIgnoreCase("c")) {
                String firstName = Validator.getStringInput(sc, "Enter first name: ");
                String lastName = Validator.getStringInput(sc, "Enter last name: ");
                String email = Validator.getStringInput(sc, "Enter email address: ");
                String custNumber = Validator.getStringInput(sc, "Customer number: ");
                //System.out.println(custNumber);
            } else if (userType.equalsIgnoreCase("e")) {
                String firstName = Validator.getStringInput(sc, "Enter first name: ");
                String lastName = Validator.getStringInput(sc, "Enter last name: ");
                String email = Validator.getStringInput(sc, "Enter email address: ");
                int empSoc = Validator.getInt(sc, "Social security number: ");
            }
            choice = Validator.getStringContinue(sc, "Continue? (y/n): ");
        }
    }
}

abstract class Person {

    private String firstName;
    private String lastName;
    private String eMail;

    public Person() {
        firstName = "";
        lastName = "";
        eMail = "";
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String geteMail() {
        return eMail;
    }

    public void seteMail(String eMail) {
        this.eMail = eMail;
    }

    @Override
    public String toString() {
        return "Name: " + this.firstName + this.lastName + "\n" + "E-mail: "
                + this.eMail;
    }

    abstract String getDisplayText();
}

abstract class Customer extends Person {

    private String customerNumber;

    public Customer() {
        super.toString();
        customerNumber = "";
    }

    public void setcustomerNumber(String customerNumber) {
        this.customerNumber = customerNumber;
    }

    public String getcustomerNumber() {
        return customerNumber;
    }

    @Override
    public String toString() {
        return super.toString() + "Social Security number: " + customerNumber
                + "\n";
    }
}

abstract class Employee extends Person {

    private String socNumber;

    public Employee() {
        super.toString();
        socNumber = "";
    }

    public void setsocNumber(String socNumber) {
        this.socNumber = socNumber;
    }

    public String getsocNumber() {
        return socNumber;
    }

    @Override
    public String toString() {
        return super.toString() + "Social Security number:      " + socNumber
                + "\n";
    }
}

class Validator {
    public static String getStringContinue(Scanner sc, String prompt) {
        boolean isValid = false;
        String s = "";
        while (isValid == false) {
            System.out.print(prompt);
            if (sc.hasNext("y")) {
                s = sc.nextLine(); // read entire line
                isValid = true;
            } else if (sc.hasNext("n")) {
                s = sc.nextLine();
                isValid = true;
            } else {
                s = sc.nextLine();
                isValid = false;
                System.out.println("Error! Invalid string value. Try again.");
            }
        }
        return s;
    }

    public static String getStringInput(Scanner sc, String prompt) {
        boolean isValid = false;
        String s = "";
        while (isValid == false) {
            System.out.print(prompt);
            if (sc.hasNext()) {
                s = sc.nextLine(); // read entire line
                isValid = true;
            } else {
                System.out.println("Error! Invalid string value. Try again.");
            }
        }
        return s;
    }

    public static int getInt(Scanner sc, String prompt) {
        int i = 0;
        boolean isValid = false;
        while (isValid == false) {
            System.out.print(prompt);
            if (sc.hasNextInt()) {
                i = sc.nextInt();
                isValid = true;
            } else {
                System.out.println("Error! Invalid integer value. Try again.");
            }
            sc.nextLine(); // discard any other data entered on the line
        }
        return i;
    }

    public static int getInt(Scanner sc, String prompt, int min, int max) {
        int i = 0;
        boolean isValid = false;
        while (isValid == false) {
            i = getInt(sc, prompt);
            if (i <= min) {
                System.out.println("Error! Number must be greater than " + min
                        + ".");
            } else if (i >= max) {
                System.out.println("Error! Number must be less than " + max
                        + ".");
            } else {
                isValid = true;
            }
        }
        return i;
    }

    public static double getDouble(Scanner sc, String prompt) {
        double d = 0;
        boolean isValid = false;
        while (isValid == false) {
            System.out.print(prompt);
            if (sc.hasNextDouble()) {
                d = sc.nextDouble();
                isValid = true;
            } else {
                System.out.println("Error! Invalid decimal value. Try again.");
            }
            sc.nextLine(); // discard any other data entered on the line
        }
        return d;
    }

    public static double getDouble(Scanner sc, String prompt, double min,
            double max) {
        double d = 0;
        boolean isValid = false;
        while (isValid == false) {
            d = getDouble(sc, prompt);
            if (d <= min) {
                System.out.println("Error! Number must be greater than " + min
                        + ".");
            } else if (d >= max) {
                System.out.println("Error! Number must be less than " + max
                        + ".");
            } else {
                isValid = true;
            }
        }
        return d;
    }
}
4

1 回答 1

0

我们先来看看打印方法:

public static void print(Person person) {
    System.out.println(person.toString());
}

现在你的 Person 类中需要一个抽象方法:

public abstract String getDisplayText(Person person);

您将在 Employee 和 Customer 类中覆盖它:

// Employee
@Override
public String getDisplayText(Person person) {
    return ((Employee)person).toString();
}

和:

// Customer
@Override
public String getDisplayText(Person person) {
    return ((Customer)person).toString();
}

他们希望您将方法抽象化,因为每个实现都是不同的——客户有一个客户 ID,员工有一个 Soc 号。

现在让我们解释一下静态方法和抽象方法:静态方法和抽象方法之间的区别在于抽象方法只是一个标头——实现依赖于子类。而静态方法意味着您可以在不首先创建对象的情况下调用它。

这个特定的静态方法的使用非常简单。假设你有:

Person john = new Customer();
... // Fill john with data
Person.print(john);

在我看来,您对所有这些抽象和静态的东西都有些困惑。抽象类是不能创建对象的类。在您的示例中,您有一个抽象类 Person。该类是抽象的,因为您需要客户或员工(两者都是人,因此继承自 Person 类),但您不想存储关于不是这些人的人的信息。这也是你不能这样做的原因:

Person john = new Person(); // Build error

但是您可以(并且应该)这样做:

Person john = new Employee(); // Will compile and run
于 2013-02-16T20:47:45.510 回答