本文介绍了使用 List<E> 类型的私有变量帮助 Java 中的抽象类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

类:DBList

import java.util.List;公共抽象类 DBList {私人列表<UserProfile>列表名称;私人列表<UserProfile>列表SSN;公共列表获取列表名称(){返回 this.listName;}公共列表getListSSN() {返回 this.listSSN;}public void setListName(List listName) {this.listName = listName;}public void setListSSN(ListlistSSN) {this.listSSN = listSSN;}}

类:DBListArray

import java.util.ArrayList;公共类 DBListArray 扩展 DBList {公共 DBListArray() {super.setListName(new ArrayList());super.setListSSN(new ArrayList());}public DBListArray(ArrayList listName, ArrayList listSSN) {super.setListName(listName);super.setListSSN(listSSN);}公共 DBListArray(DBListArray dbListArray) {super.setListName(dbListArray.getListName());super.setListSSN(dbListArray.getListSSN());}}

类:DBListLinked

import java.util.LinkedList;公共类 DBListLinked 扩展 DBList {公共 DBListLinked() {super.setListName(new LinkedList());super.setListSSN(new LinkedList());}public DBListLinked(LinkedList listName, LinkedList listSSN) {super.setListName(listName);super.setListSSN(listSSN);}公共 DBListLinked(DBListLinked dbListLinked) {super.setListName(dbListLinked.getListName());super.setListSSN(dbListLinked.getListSSN());}}

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

2) 对我来说,在 DBList 中有构造函数并在子类,但我不能这样做,因为我不能用 new List() 初始化变量.

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

解决方案

你不应该需要以 LinkedListArrayList 开始的子类和.使用 List 非常好,推荐使用(参见 Effective Java 2nd Edition,Item 52:通过接口引用对象).

请记住 LinkedList实现 ListArrayList;实现了 List,所以如果你取一个 List,你可以同时取 LinkedListArrayList 已经(以及 List 的所有其他实现者).

关于clone()

现在很好理解 clone() 在 Java 中被严重破坏,不应该使用(参见Effective Java 2nd Edition,Item 11:Override clone judiciously).

来自对作者 Josh Bloch 的采访:

如果你读过我书中关于克隆的文章,特别是如果你读了两行之间,你就会知道我认为 clone 被深深地破坏了 [...] 很少有我不再使用 Cloneable 的东西 [...] 很遗憾 Cloneable 被破坏了,但它发生了.

相关问题

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?

解决方案

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:

Related questions

这篇关于使用 List&lt;E&gt; 类型的私有变量帮助 Java 中的抽象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 09:14