[英]How can I finish the compareTo(Object o) method here?
This is the abstract class: 这是抽象类:
public abstract class AbstractRecord {
/**
* Simple getter for the similarity
*
* @return <code>int</code> containing the similarity
*/
public abstract int getSimilarity();
/**
* Simple getter for the title <code>String</code>
*
* @return <code>String</code> containing the title
*/
public abstract String getTitle();
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "["+this.title+":"+this.similarity+ "]";
}
}
and this is the extended class from it: 这是它的扩展类:
public class Record extends AbstractRecord implements Comparable<Record>{
private int similarity;
private String title;
public Record(String title, int similarity) throws IndexException {
if (title == null || title.isEmpty() || similarity < 0) {
throw new IndexException("Missing title");
}
this.similarity = similarity;
this.title = title;
}
@Override
public int compareTo(Record r) {
int result;
result = r.compareTo( //what should I put here? );
return result;
}
@Override
public int getSimilarity() {
return similarity;
}
@Override
public String getTitle() {
return title;
}
}
Just fixed super variable problem and here's a new problem.. How can I fixed the compareTo() method here, trying to compare the input Record r with the local one. 只是解决了超变量问题,这是一个新问题。我如何在这里解决compareTo()方法,尝试将输入Record r与本地方法进行比较。 Thank you!
谢谢!
private int similarity;
private String title;
No need to declare title and similarity in subclass you can initialize it using super keyword 无需在子类中声明标题和相似性,您可以使用super关键字对其进行初始化
Your title
and similarity
members of the Record class hide the members of the same name of AbstractRecord
. 您的Record类的
title
和similarity
成员隐藏与AbstractRecord
相同名称的成员。
If all AbstractRecord
s have a title
and similarity
, it would make more sense to implement getTitle()
and getSimilarity()
in AbstractRecord
, instead of in Record
. 如果所有
AbstractRecord
都具有title
和similarity
,则在AbstractRecord
中而不是在Record
实现getTitle()
和getSimilarity()
会更有意义。
Make your fields of abstract class protected
or public
. 使您的抽象类领域
protected
或public
。 And for security and access permission's sake, protected
is suggested. 并且出于安全性和访问权限的考虑,建议使用
protected
的。
your variables are automatically inherited. 您的变量会自动继承。 There is no need to declare it in the subclass.
无需在子类中声明它。 It is not going to override the superclass variable
它不会覆盖超类变量
public class Record extends AbstractRecord implements Comparable<Record>{
//your methods here
}
Above code should suffice 上面的代码就足够了
Both the fields of base class will be inherited to child class. 基类的两个字段都将继承到子类。 You simply need to use
super
keyword to access fields of Base class as: 您只需要使用
super
关键字来访问Base类的字段,如下所示:
super.similarity
and super.title
super.similarity
和super.title
first of all you have to initialize the parent class( AbstractRecord ) variable it can by done in parent class constructor or in child class by super keyword 首先,您必须初始化父类( AbstractRecord )变量,它可以通过父类构造函数或子类中的super关键字完成
super.title = "value";
This same keyword you can use to access it too. 您也可以使用同一关键字来访问它。
public String getParentTitle(){
return super.title;
}
You should not redefined the title
and similarity
attributes in the subclass, and making abstract
getters in the class where the actual attributes are defined seems over-complicated. 您不应在子类中重新定义
title
和similarity
属性,并且在定义实际属性的类中使abstract
吸气剂显得过于复杂。 Then as a rule of thumb, attributes in a class should be initialise in the class. 然后,根据经验,应该在类中初始化类中的属性。
Sub-classes inherits all public
and protected
members (methods and attributes). 子类继承所有
public
成员和protected
成员(方法和属性)。 It means you can use them without having to redeclare it. 这意味着您无需重新声明即可使用它们。
In the below simplified version of your code, I kept parameter checking in the subclass constructor, assuming you can have different subclasses with different constraints. 在下面的代码简化版中,假设子类可以具有不同的约束,但我将参数检查保留在子类构造函数中。 Note that if the comparison of records is only on
title
and similarity
, you can implement it in AbstractRecord
. 请注意,如果记录的比较仅基于
title
和similarity
,则可以在AbstractRecord
实现它。
public abstract class AbstractRecord {
protected String title;
protected int similarity;
protected AbstractRecord(String title, int similarity) {
this.title = title;
this.similarity = similarity;
}
public int getSimilarity() {return similarity;}
public String getTitle() {return title;}
public String toString() {
return "["+this.title+":"+this.similarity+ "]";
}
// some abstract methods
}
public class Record extends AbstractRecord implements Comparable<Record>{
public Record(String title, int similarity) throws IndexException {
super(title, similarity);
if (title == null || title.isEmpty() || similarity < 0) {
throw new IndexException("Missing title");
}
}
@Override
public int compareTo(Record r) {
return 0;
}
// implementation of abstract methods from AbstractRecord
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.