gpt4 book ai didi

java - 在父类中插入数组列表以用子类覆盖

转载 作者:行者123 更新时间:2023-12-01 13:28:36 24 4
gpt4 key购买 nike

我是 OOP 和一般编程的新手。我在如何将东西放入父类中并从其他类和主类中调用它们时遇到了麻烦。

我在 main 中有以下 arraylist 创建者,但感觉真的是 OOP,这些应该位于父类和子类中,并且只是从 main 调用。这是正确的吗?有人可以帮助我解决这个问题吗?

如何获取父类中的arraylist,然后从main中正确调用它?

这就是我的主要内容:

package ooo1;

import java.util.ArrayList;
import java.util.Scanner;

public class ContactList {

public static void main(String[] args) {

Scanner input1 = new Scanner(System.in);
int type = 0;
while(type != 5){
System.out.println("Please select an option:");
System.out.println("Personal Contact: Enter 1");
System.out.println("Business Contact: Enter 2");
System.out.println("Display Personal Contacts: Enter 3");
System.out.println("Display Business Contacts: Enter 4");
System.out.println("5 to quit");

type = input1.nextInt();

if(type == 5){
System.out.println("Goodbye");
break;
}

ArrayList<Contact> contacts = new ArrayList<Contact>();
Scanner input = new Scanner(System.in);
System.out.println("Please enter ContactId : ");
String contactId = input.nextLine();
System.out.println("Please enter First Name : ");
String firstName = input.nextLine();
System.out.println("Please enter Last Name : ");
String lastName = input.nextLine();
System.out.println("Please enter Address : ");
String address = input.nextLine();
System.out.println("Please enter Phone Number : ");
String phoneNumber = input.nextLine();
System.out.println("Please enter Email Address : ");
String emailAddress = input.nextLine();

if(type == 1){
System.out.println("Please enter Birthday: ");
String dateofBirth = input.nextLine();
Contact pcontact = new PersonalContact(contactId, firstName, lastName, address, phoneNumber, emailAddress, dateofBirth);
contacts.add(pcontact);
}

else if(type == 2){
System.out.println("Please enter Job Title: ");
String jobTitle = input.nextLine();
System.out.println("Please enter Organization: ");
String organization = input.nextLine();
Contact bcontact = new BusinessContact(contactId, firstName, lastName, address, phoneNumber, emailAddress, jobTitle, organization);
contacts.add(bcontact);
}

}
}


}

这是我为父类所做的:

package ooo1;

import java.util.ArrayList;
import java.util.Scanner;

public abstract class Contact {

String contactId;
String firstName;
String lastName;
String address;
String phoneNumber;
String emailAddress;

public Contact(String contactId,String firstName,String lastName, String address, String phoneNumber, String emailAddress)
{
this.contactId = contactId;
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
this.phoneNumber = phoneNumber;
this.emailAddress = emailAddress;
}
public void setContactId(String input){
this.contactId = input;
}
public String getContactId(){
return contactId;
}

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

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

public void setAddress(String input){
this.address = input;
}
public String getAddress(){
return address;
}

public void setPhoneNumber(String input){
this.phoneNumber = input;
}
public String getPhoneNumber(){
return phoneNumber;
}

public void setEmailAddress(String input){
this.emailAddress = input;
}
public String getEmailAddress(){
return emailAddress;
}

void displayContacts(){
System.out.println("Contact ID:" + contactId + " First Name:" + firstName + " Last Name:" + lastName);
System.out.println("Address:" + address);
System.out.println("Phone Number:" + phoneNumber);
System.out.println("Email Address:" + emailAddress);
}

}

我的子类之一:其他相同只是添加了一些变量:Display Contact():不起作用,也不知道如何处理它。

/* * 要更改此许可证 header ,请在项目属性中选择许可证 header 。 * 要更改此模板文件,请选择“工具”|“模板 * 并在编辑器中打开模板。 */

包ooo1;

公共(public)类 PersonalContact 扩展了 Contact {

private String dateofBirth;

public PersonalContact(String contactId, String firstName, String lastName, String address, String phoneNumber, String emailAddress, String dateofBirth){

super(contactId, firstName, lastName, address, phoneNumber, emailAddress);

this.dateofBirth = dateofBirth;
}
public void setDateofBirth(String input){
this.dateofBirth=input;
}
public String getDateofBirth(){
return this.dateofBirth;
}
@Override
public void displayContacts(){
System.out.print("Personal Contacts: ");
System.out.println("Contact ID:" + contactId + " First Name:" + firstName + " Last Name:" + lastName);
System.out.println("Address:" + address);
System.out.println("Phone Number:" + phoneNumber);
System.out.println("Email Address:" + emailAddress);
System.out.println("Birthday:" + dateofBirth);
}

}

最佳答案

您可能想要这样的东西。

public class AddressBook<T extends Contact>
{

private List<T> contacts = new ArrayList<T>();

public void addContact(T contact)
{
contacts.add(contact);
}

}

您可以像这样实例化和使用此类。

AddressBook<Contact> book = new AddressBook<Contact>();
book.add(new PersonalContact(...));
book.add(new BusinessContact(...));

随着时间的推移,您可以灵活地向 AddressBook 添加与底层集合一起使用的方法。例如,您可能想要搜索具有特定名称的联系人。或者返回按特定属性排序的联系人迭代器。

关于java - 在父类中插入数组列表以用子类覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21681155/

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