帮助在Java中抽象类与类名单,LT的私有变量; E> [英] Help with abstract class in Java with private variable of type List<E>

查看:150
本文介绍了帮助在Java中抽象类与类名单,LT的私有变量; E>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它已经两年,因为我最近codeD东西在Java中,所以我的编码技能都有点生疏了。

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

我需要保存在不同的数据结构数据(用户配置文件),的ArrayList 的LinkedList ,他们都来自列表。我想避免code重复在那里我可以,我也想跟进好的Java的做法。

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.

对于这一点,我试图创建一个抽象类,其中私有变量将类型列表< E> 然后再创建2取决于子类变量的类型。

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.

事情是,我不知道如果我正确地做这个,你可以看看我的code:

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

类别: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;
    }

}

类别: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());
    }

}

类别: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)是否有这有什么意义?我究竟做错了什么?你有什么建议?

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

2)这将更有意义,对我来说,在 DBLIST 的构造函数和调用它们(以超()在子类中),但我不能这样做,因为我无法初始化新名单℃的变量; E&GT;()

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)我想尽可能地做深拷贝和我始终覆盖我的课和$的的clone()方法C.因此$ C吧。但是,这些类从未有过任何列表,集合或映射在他们身上,他们只有字符串,整数,浮点数。我该怎么做深层副本在这种情况呢?

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?

推荐答案

您应该不需要子类需要的LinkedList&LT;用户配置&GT; 的ArrayList&LT ;用户配置&GT; 开始。用户配置&GT;与列表与LT工作是罚款多,它的建议(见的有效的Java第二版,第52项:由它们的接口引用对象的)

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).

请记住,的LinkedList&LT; E&GT;实现列表&LT; E&GT; 的ArrayList&LT; E&GT;实现列表&LT; E&GT; ,所以如果你把一个列表&LT; E&GT; ,可以采取两种的LinkedList&LT; E&GT; 的ArrayList&LT; E&GT; 已经(和的其他所有实现者名单&LT; E&GT; 出那里)。

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).

这是很现在可以理解,的clone()深深打破在Java中,不应使用(见的有效的Java第二版,第11项:覆盖克隆明智的)。

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).

与笔者乔希布洛赫接受记者采访时:

如果你读过有关克隆在我的书,特别是如果你在字里行间的项目,你就会知道,我认为克隆深深打破[.. ]有对我用很少的东西 Cloneable的了[...]这是一个耻辱, Cloneable的是打破,但它发生。

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

  • Deep clone utility recomendation recomendation
  • Java: how to clone ArrayList but also clone its items?
  • Why people are so afraid of using clone() (on collection and JDK classes) ?
  • How to properly override clone method?
  • clone() vs copy constructor vs factory method??
  • 这篇关于帮助在Java中抽象类与类名单,LT的私有变量; E&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆