使用 List类型的私有变量来帮助了解 Java 中的抽象类
自从我上次用 Java 编写代码以来已经有两年了,所以我的编码技能有点生疏了。
我需要将数据(用户配置文件)保存在不同的数据结构中,ArrayList
和 LinkedList
,它们都来自 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要以
LinkedList
和ArrayList
开头的子类。使用List
非常好,建议使用(请参阅Effective Java 第 2 版,第 52 项:通过接口引用对象)。请记住实现 List,因此如果您采用
LinkedList实现 List
和 ArrayListList
,则可以同时采用LinkedList
和ArrayList。
已经(以及List
的所有其他实现者)。关于
clone()
现在已经很好理解,
clone()
在 Java 中已经严重损坏,不应该使用(参见Effective Java 2nd Edition,Item 11:明智地覆盖克隆)。来自对作者 Josh Bloch 的采访:
相关问题
推荐
You shouldn't need subclasses that takes
LinkedList<UserProfile>
andArrayList<UserProfile>
to begin with. Working withList<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>
, andArrayList<E> implements List<E>
, so if you take aList<E>
, you can take bothLinkedList<E>
andArrayList<E>
already (and all other implementors ofList<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
recomendation