java - 更新给定父类(super class)的子类

标签 java oop inheritance

问题描述

我有一个摘要Paper包含所有论文的共同属性的类,以及一个或多个为该类型的论文添加附加信息的子类。然后我有一个 HashMap<String, Paper>存储多篇论文。

我的应用程序允许用户通过提供 pid 来更新论文然后提供要更新的属性和值。我遇到的问题是当我只有父类(super class)时如何更新子类的属性。

处理这种情况的最佳方法/做法是什么?

类结构

public abstract class Paper {
    String pid;
    String title;
    String author;
}


public class Publication extends Paper {
    int pages;
}

public class PHDThesis extends Paper {
    String supervisor;
}

我目前的尝试

这就是我目前拥有的**,它可以通过使用 instance of 来工作;但我觉得应该有更好的方法来做到这一点。

import java.util.*;

public class App {
    public static abstract class Paper {

        private String title;
        private String author;

        public Paper(String title, String author) {
            this.title = title;
            this.author = author;
        }

        public void update(String title, String author) {
            this.title = title;
            this.author = author;
        }
    }


    public static class Publication extends Paper {

        private int pages;

        public Publication(int pages, String title, String author) {
            super(title, author);
            this.pages = pages;
        }

        public void update(String title, String author, int pages) {
            super.update(title, author);
            this.pages = pages;
        }

    }

    public static class PHDThesis extends Paper {

        private String supervisor;

        public PHDThesis(String supervisor, String title, String author) {
            super(title, author);
            this.supervisor = supervisor;
        }

        public void update(String title, String author, String supervisor) {
            super.update(title, author);
            this.supervisor = supervisor;
        }
    }

    public static void main(String[] args) {
        HashMap<String, Paper> papers = new HashMap<String, Paper>();

        papers.put("P001", new PHDThesis("My Super", "My PHD Title", "My Author"));
        papers.put("P002", new Publication(22, "My Pub Title", "My Author"));

        Paper p = papers.get("P001");

        if (p instanceof PHDThesis) {
            ((PHDThesis)p).update("New Title", "New author", "New Super");
        } else if (p instanceof Publication) {
            ((Publication)p).update("New Title", "New author", 33);
        }
    }
}

** 减少了测试代码,实际代码更复杂,布局也更好。

最佳答案

您可以创建一个名为 UpdateBundle 的对象,其中包含每个属性的 getter。

然后 Paper 类将有一个方法 update(UpdateBundle),每个 child 将以不同的方式实现它。

您只需为每个 child 调用该方法,他们就会知道如何处理它。

另外,我不明白为什么论文类是抽象的。您似乎没有抽象方法。

public abstract class Paper {
    String pid;
    String title;
    String author;

    public void update(PaperUpdateBundle bundle)
    {
        pid = bundle.getPID();
        title = budnle.getTitle();
        author = bundle.getAuthor();
    }
}


public class Publication extends Paper {
    int pages;

    public void update(PaperUpdateBundle bundle)
    {
       super.update(bundle);
       pages = bundle.getPages();
    }
}

public class PHDThesis {
    String supervisor;


    public void update(PaperUpdateBundle bundle)
    {
       super.update(bundle);
       supervisor = bundle.getSupervisor();
    }
}

public interface PaperUpdateBundle
{
    String getPID();
    String getTitle();
    String getAuthor();
    int getPages();
    String getSupervisor();
}

关于java - 更新给定父类(super class)的子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9659423/

相关文章:

python - 创建对象属性的引用列表

oop - 猴子修补/类重新开放真的是反射的例子吗?

javascript - JS 继承 : calling the parent's function from within the child's function

java - 是否可以使用 JMeter 进行复杂的测试场景?

java - Socket编程——客户端(linux)、服务端(Windows)

c++ - 是抽象类还是纯虚(接口(interface))?

java - 自动调用父类(super class)方法

java - 这里的继承是怎么回事?

java - 序列化包含不可序列化类的类

java - 将整数转换为日期