使用调用另一个类的getter,并将该类作为格式化字符串返回?

时间:2017-01-19 21:27:58

标签: java class oop object methods

我正在努力学习OOP,我有一个有3个班级的程序;人,人事程序(主要)和日期。 Person程序包含新Person的构造函数,包括它们的名字,姓氏,中间名首字母和出生日期。出生日期的类型为Date,而在Date类中,我有一个构造函数,它为生日的日期,月份和年份提供3个整数。我在理解这一切如何协同工作方面遇到了一些麻烦,以及如何正确地调用birthDate。我将使用我的教授为我们设计程序的UML图表。人们的对象就是这样创建的:

 people = new Person[5];
 people[0] = new Person("Smith", "John", 'T', new Date(4, 2, 1992));

现在我可以理解getter如何为名称和中间名首字母工作。我只是从Person类调用getter并返回所选的。我不明白的是,我在呼唤BirthDate。如果我像其他人一样叫我的生日的吸气剂:

public Date getBirthDate() {
return BirthDate;
}

究竟是什么回归?是否需要调用Date类才能获得某个生日?由于birthdate是用另一个类创建的,它是否会在People对象之外(或内部)创建另一个对象?

我想知道,因为我正在尝试将BirthDate格式化为字符串,因此当用户询问人员信息时,它可以以正确的格式显示。所以在Date类里面我有

public String displayFormattedDate() {
    return String.format("%s/%s/%s", String.valueOf(year), String.valueOf(month), String.valueOf(day));
}

那么,我会从Person类中的getter调用它吗?如果是这样我该怎么办?我是否需要在方法内初始化一个新的Date对象才能调用它?

编辑:我的课程代码

人:

public class Person {

    private String lastName;
    private String firstName;
    private char middleInit;
    private Date birthDate;

    public Person() {}




     public Person(String lastName, String firstName, char middleInit, Date birthDate) {
        //Person p = new Person("Smith", ... );
        setLastName(lastName);
        setFirstName(firstName);
        setMiddleInit(middleInit);
        setBirthDate(birthDate);

    }


    public String getLastName() {
        return lastName;
    }


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


    public String getFirstName() {
        return firstName;
    }


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


    public char getMiddleInit() {
        return middleInit;
    }


    public void setMiddleInit(char middleInit) {
        this.middleInit = middleInit;
    }


    public Date getBirthDate() {
        Date birthday = new Date();
        return birthDate.displayFormattedDate;

    }


    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }


    public String getFullName() {
        return String.format("%s, %s %s.", this.lastName, this.firstName, this.middleInit);
    }

}

日期:

public class Date {

    private int month;
    private int day;
    private int year;

    public Date(int theMonth, int theDay, int theYear) {
        setMonth(theMonth);
        setDay(theDay);
        setYear(theYear);
    }


    public int getMonth() {
        return month;
    }


    public int getDay() {
        return day;
    }


    public int getYear() {
        return year;
    }


    public void setMonth(int month) {
        if ((month > 0) && (month < 13)) {
            this.month = month;
        } else {
            invalidDate();
        }
    }

    /**
     * @param day the day to set
     */
    public void setDay(int day) {
        int[] days = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

        if (month == 2 && day == 29 && (year % 400 == 0
                || (year % 4 == 0 && year % 100 != 0))) {
            //day is valid
            this.day = day;
        } else if (day < 1 || day > days[month]) {
            // day is not valid default to 1
            day = 1;
            this.day = day;
        } else {
            //day is valid
            this.day = day;
        }
    }

    /**
     * @param year the year to set
     */
    public void setYear(int year) {
        this.year = year;
    }

    public void invalidDate() {
        System.out.println("Please input a date number from 1 to 12");

    }


    public String displayFormattedDate() {
        return String.format("%s/%s/%s", String.valueOf(year), String.valueOf(month), String.valueOf(day));
    }
}

Personprgraom(主)

import java.util.Scanner;

public class PersonProgram {

    Scanner input;
    Person[] people;

    public PersonProgram() {
        input = new Scanner(System.in);
    }

    public void getData() {
        people = new Person[5];
        people[0] = new Person("Smith", "John", 'T', new Date(4, 2, 1992));
        people[1] = new Person("Terry", "TwoToots", 'G', new Date(9, 14, 1990));
        people[2] = new Person("MIke", "Mellow", 'M', new Date(3, 10, 1985));
        people[3] = new Person("Steve", "Ramadam", 'N', new Date(4, 20, 1905));
        people[4] = new Person("Bizkit", "Limp", 'H', new Date(12, 25, 1972));

    }

    public int showMenu() {
        System.out.println("1. Display all names.");
        System.out.println("2. Display info for person by number");
        System.out.println("3. Edit information for person by number");
        System.out.println("4. Exit");
        System.out.println("Choice: ");

        int selection = input.nextInt();
        return selection;
    }

    public void executeChoices(int choice) {
        switch (choice) {
            case 1:
                menuOption1();
                break;
            case 2:
                menuOption2();
                break;
            case 3:
                menuOption3();
                break;
            case 4:
            //menuOption4();
        }
    }

    public void menuOption1() {
        for (int counter = 0; counter < people.length; counter++) {
            System.out.println(counter + 1 + ". " + people[counter].getFullName());
        }

    }

    public void menuOption2() {
        System.out.println("Enter person number: ");
        int selection = input.nextInt();

        System.out.println("Full Name: " + people[selection].getFullName());
        System.out.println("Birth Date: " + people[selection].getBirthDate());
    }

    public void menuOption3() {
        System.out.println("Please select a person to edit");

        for (int counter = 0; counter < people.length; counter++) {
            System.out.println(counter + 1 + ". " + people[counter].getFullName());
        }
        int choice;
        choice = input.nextInt();

        System.out.println("What would you like to edit?");
        System.out.println("1. First Name");
        System.out.println("2. Last Name");
        System.out.println("3. Middle initial");
        System.out.println("4. Birth Date");
        System.out.println("5. Cancel");

        int menuselect = input.nextInt();

        if (menuselect == 1) {
            System.out.println("Enter the new first name: ");
            people[choice - 1].setFirstName(input.next());
        }

        if (menuselect == 2) {
            System.out.println("Enter the new last name: ");
            people[choice - 1].setLastName(input.next());
        }

        if (menuselect == 3) {
            System.out.println("Enter the new Middle Initial: ");
            people[choice - 1].setMiddleInit(input.next().charAt(0));
        }

        if (menuselect == 4) {
            System.out.println("Enter the new birth date: ");
            System.out.println("Please enter new Year: ");
            int year = input.nextInt();
            System.out.println("Please enter new month");
            int month = input.nextInt();
            System.out.println("Please enter new day");
            int day = input.nextInt();
            Date birthDate = new Date(month, day, year);
            people[choice - 1].setBirthDate(birthDate);


        }

        if (menuselect == 5) {
            System.out.println("Please select a person to edit");

            for (int counter = 0; counter < people.length; counter++) {
                System.out.println(counter + 1 + ". " + people[counter].getFullName());
            }
        }
    }

    public void menuOption4() {
        System.exit(0);
    }

    public static void main(String[] args) {

        PersonProgram p = new PersonProgram();
        p.getData();
        int userinput = p.showMenu();
        p.executeChoices(userinput);

    }

}

2 个答案:

答案 0 :(得分:0)

我认为你会想在Person类中调用toString()的另一个方法来执行以下操作

public String toString()
{
    return (getFullName() + " DOB: " + birthDate.displayFormattedDate());
}

根据您的示例返回一个字符串

"Smith, John T. DOB: 4/2/1992"

答案 1 :(得分:0)

getters 的全部目的是它们返回对象的属性而不做任何更改。因此,getter(通常)不会创建任何类的新实例。 (我通常写&#39;因为像其他任何方法一样,getter可以实现,但他们不应该......)

birthDate属性的getter返回通过构造函数传递给对象的对象(在您的示例new Date(4, 2, 1992)中)。

是的,对于显示,您必须使用系统库中的SimpleDateFormat类来为其应用适当的格式。