面向对象的编程 - 创建子类

时间:2017-11-29 21:03:19

标签: java inheritance

我有一个csv文件,其中包含医生详细信息:姓名,地址,城市,首选联系人,联系人ID和专业。

我已经创建了一个医生列表,我想在其中存储我的医生对象。我决定拆分scanner.nextLine()并将每个值解析为Doctor实例。

这对我有用,所有数值都被正确读取并分配给与医生相关的正确变量,即名字,姓氏等,但唯一的问题是如果医生专业是手术,我需要存储一个附加值,即认证日期。

为了做到这一点,我创建了一个扩展的外科医生课程并试图使用继承但在这个阶段迷路了,并且没有关于如何解决这个问题的想法。

有人能够告诉我我可以改变什么来使这项工作吗?

public class Doctor 
{

    protected String name;
    protected String surname;
    protected String address;
    protected String city;
    protected String preferredContact;
    protected String contactID;
    protected String specialism;

    public Doctor()
    {
        this.name="";
        this.surname="";
        this.address="";
        this.city="";
        this.preferredContact="";
        this.contactID="";
        this.specialism="";
    }

    public Doctor(String name,String surname, String address, String city, String preferredContact, String contactID, String specialism)
    {
        this.name=name;
        this.surname=surname;
        this.address=address;
        this.city=city;
        this.preferredContact=preferredContact;
        this.contactID=contactID;
        this.specialism=specialism;
    }

    public void setName(String name)
    {
        this.name=name;
    }

    public String getName()
    {
        return name;
    }

    public void setSurname(String surname)
    {
        this.surname=surname;
    }

    public String getSurname()
    {
        return surname;
    }

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

    public String getAddress()
    {
        return address;
    }

    public void setCity(String city)
    {
        this.city=city;
    }

    public String getCity()
    {
        return city;
    }

    public void setPreferredContact(String preferredContact)
    {
        this.preferredContact=preferredContact;
    }

    public String getPreferredContact()
    {
        return preferredContact;
    }

    public void setContactID(String contactID)
    {
        this.contactID=contactID;
    }

    public String getContactID()
    {
        return contactID;
    }

    public void setSpecialism(String specialism)
    {
        this.specialism=specialism;
    }

    public String getSpecialism()
    {
        return specialism;
    }

    @Override
    public String toString()
    {
        return "\nName:"+getName()
            +"\nSurname: "+getSurname()
            +"\nAddress: "+getAddress()
            +"\nCity: "+getCity()
            +"\nPreferred Means of Contact: "+getPreferredContact()
            +"\nContact ID: "+getContactID()
            +"\nSpecialism: "+getSpecialism()
            +"\n";

    }

}

public class Surgeon extends Doctor
{

    protected String certificationDate;

    public Surgeon()
    {
        super();
        certificationDate="";
    }

    public Surgeon(String name,String surname, String address, String city, String preferredContact, String contactID, String specialism, String certificationDate)
    {
        super(name,surname,address,city,preferredContact,contactID,specialism);
        this.certificationDate=certificationDate;
    }

    public String getCertificationDate()
    {
        return certificationDate;
    }

    @Override
    public String toString()
    {
        return "\nCertificationDate: "+certificationDate +"\n";
    }
}

public class DoctorImport 
{

    public static void main (String[]args) 
    {   
        int index = 0;

        List<Doctor> doctorsList = new ArrayList<>();

        try
        {
            Scanner scanner=new Scanner(new File("DoctorsFile.csv"));
            Scanner dataScanner;

            while (scanner.hasNextLine())
            {
                dataScanner=new Scanner(scanner.nextLine());
                dataScanner.useDelimiter(",");

                Doctor myDoctor=new Doctor();
                Surgeon mySurgeon=new Surgeon();

                while(dataScanner.hasNext())
                {
                    String data= dataScanner.next();

                    switch (index) 
                    {
                        case 0:
                            myDoctor.setName(data);
                            break;

                        case 1:
                            myDoctor.setSurname(data);
                            break;

                        case 2:
                            myDoctor.setAddress(data);
                            break;

                        case 3:
                            myDoctor.setCity(data);
                            break;

                        case 4:
                            myDoctor.setPreferredContact(data);
                            break;

                        case 5:
                            myDoctor.setContactID(data);
                            break;

                        case 6:
                            myDoctor.setSpecialism(data);
                            break;

                         case 7:
                            mySurgeon.certificationDate=data;
                            break;

                     }
                    index++;
                }
                doctorsList.add(myDoctor);

                if((myDoctor.specialism).equals("Surgery"))
                {
                    doctorsList.add(mySurgeon);
                }

                index=0;  
            }
            System.out.print(doctorsList);
        } 

        catch (FileNotFoundException ex) 
        {
            System.out.print("Error, unable to locate the CSV File!");
        }

    }

}

3 个答案:

答案 0 :(得分:0)

我猜你的代码有一些错误。

其中一个是:

您在阅读文件时初始化了Doctor对象,但是如果它具有专业性,那么您将读取数据并将Surgen对象添加到列表中。但是您的外科医生对象没有信息,也没有链接到Doctor对象。

你的代码在某些时候有效吗?你测试过吗? 如果是这样,我们可以从那时开始工作。

问候。

答案 1 :(得分:0)

一种方法是首先收集所有数据,然后再创建正确类型的对象。我建议阅读工厂模式,因为它封装了关于构建哪种类型对象的决策。

例如:

    doctorsList.add(buildDoctor(name, surname, address, city, preferredContact, contactID, specialism, certificationDate));

工厂模式可用于实现决定要创建哪个对象的最后一部分:

private Doctor buildDoctor(String name, String surname, String address, String city, String preferredContact, String contactID, String specialism, String certificationDate) {
    if (specialism.equals("surgery")) {
        return new Surgeon(name, surname, address, city, preferredContact, contactID, specialism, certificationDate);
    } else {
        return new Doctor(name, surname, address, city, preferredContact, contactID, specialism);
    }
}

...调用工厂方法:

{{1}}

答案 2 :(得分:0)

问题在于您正在创建Doctor和Surgeon实例。然后填充Doctor实例;如果您有认证日期,则将其放入(否则为空)外科医生实例中。

您需要根据某些条件创建一个或另一个实例。只是使用认证日期是否通过是不好的做法。也许专业领域会起作用。

如果您有Surgeon实例,也可以将其分配给Doctor字段。然后将在一个实例中填充所有信息。

相关问题