gpt4 book ai didi

java - 创建类似于java对象层次结构的树

转载 作者:行者123 更新时间:2023-12-01 15:13:06 25 4
gpt4 key购买 nike

我正在尝试为 Android 创建一个漫画应用程序,其中每个章节都有自己的标题、出版日期、描述等。并且每个章节都属于一个 Manga 对象。这将是章节的集合,并包括标题列表以及漫画本身的标题和作者姓名。数据本身将从不同的网页解析(但那是另一回事)。

我的困惑在于类声明。 (即实现、扩展)

我已经尝试了很多事情,但到目前为止,我的代码包括将章节作为内部类,如下所示:

public abstract class Manga implements MangaList {
public String name;
public String author;
public int chapters;

// names of the XML tags
static final String CHANNEL = "channel";
static final String PUB_DATE = "pubDate";
static final String DESCRIPTION = "description";
static final String LINK = "link";
static final String TITLE = "title";
static final String ITEM = "item";

private final URL feedUrl;

protected Manga(String feedUrl){
try {
this.feedUrl = new URL(feedUrl);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}

protected InputStream getInputStream() {
try {
return feedUrl.openConnection().getInputStream();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

List<Chapter> Chapter;

public class Chapter implements Comparable<Chapter> {

final SimpleDateFormat FORMATTER =
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
private String title;
private URL link;
private String description;
private Date date;

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title.trim();
}
// getters and setters omitted for brevity
public URL getLink() {
return link;
}

public void setLink(String link) {
try {
this.link = new URL(link);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description.trim();
}

public String getDate() {
return FORMATTER.format(this.date);
}

public void setDate(String date) {
// pad the date if necessary
while (!date.endsWith("00")){
date += "0";
}
date = "";
try {
this.date = FORMATTER.parse(date.trim());
} catch (ParseException e) {
throw new RuntimeException(e);
}
}

public Chapter copy(){
Chapter copy = new Chapter();
copy.title = title;
copy.link = link;
copy.description = description;
copy.date = date;
return copy;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Title: ");
sb.append(title);
sb.append('\n');
sb.append("Date: ");
sb.append(this.getDate());
sb.append('\n');
sb.append("Link: ");
sb.append(link);
sb.append('\n');
sb.append("Description: ");
sb.append(description);
return sb.toString();
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((date == null) ? 0 : date.hashCode());
result = prime * result
+ ((description == null) ? 0 : description.hashCode());
result = prime * result + ((link == null) ? 0 : link.hashCode());
result = prime * result + ((title == null) ? 0 : title.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Chapter other = (Chapter) obj;
if (date == null) {
if (other.date != null)
return false;
} else if (!date.equals(other.date))
return false;
if (description == null) {
if (other.description != null)
return false;
} else if (!description.equals(other.description))
return false;
if (link == null) {
if (other.link != null)
return false;
} else if (!link.equals(other.link))
return false;
if (title == null) {
if (other.title != null)
return false;
} else if (!title.equals(other.title))
return false;
return true;
}

public int compareTo(Chapter another) {
if (another == null) return 1;
// sort descending, most recent first
return another.date.compareTo(date);
}
}

我的问题是,这是否是一种合适的格式,或者是否有更简单的方法来创建漫画列表,每个漫画列表都有自己的一组章节?

编辑:我查了一下并决定使用 SQLite 数据库将是跟踪我将要解析的大量数据的更简单的方法。

这样我就可以维护两个数据库。一个用于漫画标题和作者,另一个用于章节和相关信息。相关章节将通过引用 ID 链接到每个表格中的漫画。

最佳答案

我绝对认为有一种更简单的方法可以做到这一点;然而,这实际上取决于您的总体目标是什么。如果您尝试在 list 中显示此内容,您可能会考虑使用 ListView,但如果您只是将数据用于内容,那么您可能可以执行类似于以下操作的操作你有。最终,您需要弄清楚要使用该应用程序做什么,然后才能找出实现它的最简单方法。但请记住:更容易并不总是更好。尝试从长远角度思考您的项目,例如谁将维护该项目、该项目会扩大还是缩小、以及您是否会添加功能。

至于扩展实现,它们分别是子类接口(interface),并且有不同的规则更多信息可以在这里找到:http://docs.oracle.com/javase/tutorial/java/concepts/interface.html

祝你好运!

关于java - 创建类似于java对象层次结构的树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12031653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com