gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:51:45 25 4
gpt4 key购买 nike

我正在尝试学习 OOP,并且我有一个包含 3 个类(class)的程序;人员、人员程序(主要)和日期。 Person 程序包含新 Person 的构造函数,包括名字、姓氏、中间名首字母和出生日期。出生日期是 Date 类型,在 Date 类中,我有一个构造函数,它接受 3 个整数来表示人的出生日期的年、月、日。我在理解这一切如何协同工作以及如何正确调用生日时遇到一些困难。我将使用我的教授给我们的 UML 图来设计程序。为人们创建的对象是这样的:

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

现在我可以理解 setter/getter 如何用于名称和中间名首字母了。我只需从 Person 类调用 getter,它就会返回所选的值。我不明白的是我为 BirthDate 调用的 getter 是什么。如果我像其他人一样调用我的生日的 setter/getter :

public Date getBirthDate() {
return BirthDate;
}

返回的到底是什么?是否需要调用 Date 类来获取特定的生日?由于生日是使用另一个类创建的,因此是否会在 People 对象外部(或内部)创建另一个对象?

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

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

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

编辑:我的类(class)代码

人:

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);

}

}

最佳答案

我认为您需要 Person 类中的另一个名为 toString() 的方法来执行以下操作

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

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

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

关于java - 使用调用另一个类的 getter 并将该类作为格式化字符串返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41752003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com