繁体   English   中英

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

[英]Insert array list in parent class to override with subclasses

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

我在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():也不知道该怎么做也不起作用。

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

软件包ooo1;

公共类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添加与基础集合一起使用的方法。 例如,您可能要搜索具有特定名称的联系人。 或返回按特定属性排序的Contact的迭代器。

您可以在Contact类中添加一个方法:

public void getData(){
// take in all the inputs here, so that you can directly store them in class member variables instead of passing them from main.
}

假设PersonalContact和BusinessContact是从Contact继承的类。 您可以在其中添加方法:

class PersonalContact extends Contact{  
String dateofBirth;
public void getData(){
super.getData();  //calls getData() method from base class
// take DOB as input & store it
}

对于BusinessContact类也是如此。

我建议您看一下抽象类和接口,以备将来使用。

Contact类似乎还可以。 但是ContactList没有那么多。 它应该是联系人的数据结构,因此没有理由使用主要方法。

public class ContactList {

     private ArrayList<Contact> contacts;       

     public ContactList(){
          this.contacts = new ArrayList<Contact>();
     }

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

     public Contact getContact(int index){
          return contacts.get(index);
     }

     // other methods that work with the data structure
     // f.e. searching, deleting, ...

}

然后您可以拥有一些ContactUtil类,该类将负责从用户(主要方法中的内容)读取用户的联系信息。

public final class ContactUtil {

    private ContactUtil(){} // we don't want to create instances of this class

    public static Contact readNewContact(){

         Scanner input1 = new Scanner(System.in);
         int type = 0;
         ...


         return contact;
    }   

}

最后,您将获得一些仅用于main()

public class Main {

      public static void main(String[] args){

           ContactList myContacs = new ContactList();
           myContacts.add(ContactUtil.readNewContact());

           Contact contact = ContactUtil.readNewContact();
           myContacts.add(contact); 

      }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM