使用 List类型的私有变量来帮助了解 Java 中的抽象类

发布于 2024-09-02 02:27:22 字数 2491 浏览 5 评论 0原文

自从我上次用 Java 编写代码以来已经有两年了,所以我的编码技能有点生疏了。

我需要将数据(用户配置文件)保存在不同的数据结构中,ArrayListLinkedList,它们都来自 List。我希望尽可能避免代码重复,并且我还希望遵循良好的 Java 实践。

为此,我尝试创建一个抽象类,其中私有变量的类型为 List,然后根据变量的类型创建 2 个子类。

问题是,我不知道我这样做是否正确,你可以看看我的代码:

Class: DBList

import java.util.List;

public abstract class DBList {

    private List<UserProfile> listName;
    private List<UserProfile> listSSN;

    public List<UserProfile> getListName() {
    return this.listName;
    }

    public List<UserProfile> getListSSN() {
    return this.listSSN;
    }

    public void setListName(List<UserProfile> listName) {
    this.listName = listName;
    }

    public void setListSSN(List<UserProfile> listSSN) {
    this.listSSN = listSSN;
    }

}

Class: DBListArray

import java.util.ArrayList;

public class DBListArray extends DBList {

    public DBListArray() {
    super.setListName(new ArrayList<UserProfile>());
    super.setListSSN(new ArrayList<UserProfile>());
    }

    public DBListArray(ArrayList<UserProfile> listName, ArrayList<UserProfile> listSSN) {
    super.setListName(listName);
    super.setListSSN(listSSN);
    }

    public DBListArray(DBListArray dbListArray) {
    super.setListName(dbListArray.getListName());
    super.setListSSN(dbListArray.getListSSN());
    }

}

Class: DBListLinked

import java.util.LinkedList;

public class DBListLinked extends DBList {

    public DBListLinked() {
    super.setListName(new LinkedList<UserProfile>());
    super.setListSSN(new LinkedList<UserProfile>());
    }

    public DBListLinked(LinkedList<UserProfile> listName, LinkedList<UserProfile> listSSN) {
    super.setListName(listName);
    super.setListSSN(listSSN);
    }

    public DBListLinked(DBListLinked dbListLinked) {
    super.setListName(dbListLinked.getListName());
    super.setListSSN(dbListLinked.getListSSN());
    }

}

1) 这些有什么意义吗?我做错了什么?您有什么建议吗?

2) 对我来说,将构造函数放在 DBList 中并在子类中调用它们(使用 super())会更有意义,但我无法这样做,因为我无法使用 new List() 初始化变量。

3) 我认为只要有可能就进行深度复制,因此我总是重写类的 clone() 方法并相应地对其进行编码。但这些类从来没有任何列表、集合或映射,它们只有字符串、整数、浮点数。在这种情况下如何进行深拷贝?

It's been two years since I last coded something in Java so my coding skills are bit rusty.

I need to save data (an user profile) in different data structures, ArrayList and LinkedList, and they both come from List. I want to avoid code duplication where I can and I also want to follow good Java practices.

For that, I'm trying to create an abstract class where the private variables will be of type List<E> and then create 2 sub-classes depending on the type of variable.

Thing is, I don't know if I'm doing this correctly, you can take a look at my code:

Class: DBList

import java.util.List;

public abstract class DBList {

    private List<UserProfile> listName;
    private List<UserProfile> listSSN;

    public List<UserProfile> getListName() {
    return this.listName;
    }

    public List<UserProfile> getListSSN() {
    return this.listSSN;
    }

    public void setListName(List<UserProfile> listName) {
    this.listName = listName;
    }

    public void setListSSN(List<UserProfile> listSSN) {
    this.listSSN = listSSN;
    }

}

Class: DBListArray

import java.util.ArrayList;

public class DBListArray extends DBList {

    public DBListArray() {
    super.setListName(new ArrayList<UserProfile>());
    super.setListSSN(new ArrayList<UserProfile>());
    }

    public DBListArray(ArrayList<UserProfile> listName, ArrayList<UserProfile> listSSN) {
    super.setListName(listName);
    super.setListSSN(listSSN);
    }

    public DBListArray(DBListArray dbListArray) {
    super.setListName(dbListArray.getListName());
    super.setListSSN(dbListArray.getListSSN());
    }

}

Class: DBListLinked

import java.util.LinkedList;

public class DBListLinked extends DBList {

    public DBListLinked() {
    super.setListName(new LinkedList<UserProfile>());
    super.setListSSN(new LinkedList<UserProfile>());
    }

    public DBListLinked(LinkedList<UserProfile> listName, LinkedList<UserProfile> listSSN) {
    super.setListName(listName);
    super.setListSSN(listSSN);
    }

    public DBListLinked(DBListLinked dbListLinked) {
    super.setListName(dbListLinked.getListName());
    super.setListSSN(dbListLinked.getListSSN());
    }

}

1) Does any of this make any sense? What am I doing wrong? Do you have any recommendations?

2) It would make more sense for me to have the constructors in DBList and calling them (with super()) in the subclasses but I can't do that because I can't initialize a variable with new List<E>().

3) I was thought to do deep copies whenever possible and for that I always override the clone() method of my classes and code it accordingly. But those classes never had any lists, sets or maps on them, they only had strings, ints, floats. How do I do deep copies in this situation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云淡月浅 2024-09-09 02:27:22

您不需要以 LinkedListArrayList 开头的子类。使用 List 非常好,建议使用(请参阅Effective Java 第 2 版,第 52 项:通过接口引用对象)。

请记住 LinkedList实现 List 和 ArrayList实现 List,因此如果您采用 List,则可以同时采用 LinkedListArrayList 已经(以及 List 的所有其他实现者)。


关于 clone()

现在已经很好理解,clone() 在 Java 中已经严重损坏,不应该使用(参见Effective Java 2nd Edition,Item 11:明智地覆盖克隆)。

来自对作者 Josh Bloch 的采访

如果您读过我书中有关克隆的内容,尤其是如果您阅读字里行间的内容,您就会知道我认为克隆已被严重破坏[...]很少有我不再使用 Cloneable 的东西 [...] 很遗憾 Cloneable 被破坏了,但它确实发生了。

相关问题

You shouldn't need subclasses that takes LinkedList<UserProfile> and ArrayList<UserProfile> to begin with. Working with List<UserProfile> is more than fine, it's recommended (see Effective Java 2nd Edition, Item 52: Refer to objects by their interfaces).

Do keep in mind that LinkedList<E> implements List<E>, and ArrayList<E> implements List<E>, so if you take a List<E>, you can take both LinkedList<E> and ArrayList<E> already (and all other implementors of List<E> out there).


Regarding clone()

It's well understood now that clone() is deeply broken in Java, and should not be used (see Effective Java 2nd Edition, Item 11: Override clone judiciously).

From an interview with author Josh Bloch:

If you've read the item about cloning in my book, especially if you read between the lines, you will know that I think clone is deeply broken [...] There are very few things for which I use Cloneable anymore [...] It's a shame that Cloneable is broken, but it happens.

Related questions

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文