gpt4 book ai didi

java - Java中实现类别的解决方案

转载 作者:行者123 更新时间:2023-12-01 19:11:04 25 4
gpt4 key购买 nike

You want to create a program for the management of a telephone book.For each person are provided for the following personal information:
SurnameNameTitleE-mail address (can not contain spaces and must contain the @ symbol)CompanyPosition
For every person you can store the following telephone numbers (one for each category)
HomeOfficeMobile Phone.
You can also store a list of other phone numbers. For each of the other numbers, you must store over to the phone numbera description of the number.

这是我今晚要用 Java 做的作业。我的问题是如何实现各种类别:家庭、办公室、ecc...哪个是实现这些类别的最佳解决方案?也许是一个枚举?

这是我的实现:

import java.util.*;

public class Persona {

private String Name;
private String surname;
private String title;
private String mail_addr;
private String company
private String position;
private Phone homePhone;
private Phone officePhone;
private Phone mobilePhone;
private Collection<Phone> otherphonesList

public Persona(String n,String s,String t,String m,String c,String p,Phone hp,Phone of,Phone mp,Collection<Phone> otherphones)
{
name=n;
surname=s;
title=t;
mail_addr=m;
company=c;
position=p;
homePhone=hp;
officePhone=of;
mobilePhone=mp;
otherphonesList=new ArrayList<Phone>(otherphones);
}

public String getName()
{
return name;
}

public String getSurname()
{
return surname;
}

public String getTitle()
{
return title;
}

public String getMail()
{
return mail_addr;
}

public String getCompany()
{
return company;
}

public String getPosition()
{
return position;
}
}


public class Phone {

private String phone;
private String description;

public Phone(String phone,String description)
{
this.phone=phone;
this.description=description;
}

public String getPhone()
{
return phone;
}

public String getDescription()
{
return description;
}


}

最佳答案

您可以编写一个包含您需要的字段的 PhoneBook 类:

public class PhoneBook {
private Phone homePhone;
private Phone officePhone;
private Phone mobilePhone;
private List<Phone> otherPhones;
..getters/setters..
}

public class Phone {
private String phone;
private String description;
..getters/setters..
}

关于java - Java中实现类别的解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8400122/

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