Java 如何在这里完成compareTo(objecto)方法?
这是抽象类:Java 如何在这里完成compareTo(objecto)方法?,java,Java,这是抽象类: public abstract class AbstractRecord { /** * Simple getter for the similarity * * @return <code>int</code> containing the similarity */ public abstract int getSimilarity(); /** * Simple gette
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+ "]";
}
}
这是它的扩展类:
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;
}
}
公共类记录扩展AbstractRecord实现可比较{
私有int相似性;
私有字符串标题;
公共记录(字符串标题、整数相似性)引发IndexException{
if(title==null | | title.isEmpty()| |相似度<0){
抛出新的IndexException(“缺少标题”);
}
相似性=相似性;
this.title=标题;
}
@凌驾
公共整数比较(记录r){
int结果;
result=r.compareTo(//我应该在这里放什么?);
返回结果;
}
@凌驾
公共int getSimilarity(){
返回相似性;
}
@凌驾
公共字符串getTitle(){
返回标题;
}
}
刚刚解决了超变量问题,这是一个新问题。。
我如何修复这里的compareTo()方法,尝试将输入记录r与本地记录r进行比较。
谢谢大家!
private int similarity;
private String title;
不需要在子类中声明标题和相似性,您可以使用super关键字初始化它
无需在子类中声明标题和相似性,您可以使用super关键字初始化它使抽象类
的字段受保护
或公共
。为了安全和访问权限,建议使用受保护的。将您的字段设置为抽象类受保护的或公共的。为了安全和访问权限,建议使用受保护的
。您的变量将自动继承。不需要在子类中声明它。它不会覆盖超类变量
public class Record extends AbstractRecord implements Comparable<Record>{
//your methods here
}
公共类记录扩展AbstractRecord实现可比较{
//你的方法在这里
}
上面的代码应该足够了您的变量将自动继承。不需要在子类中声明它。它不会覆盖超类变量
public class Record extends AbstractRecord implements Comparable<Record>{
//your methods here
}
公共类记录扩展AbstractRecord实现可比较{
//你的方法在这里
}
上面的代码应该足够了您的标题
和相似性
记录类的成员隐藏与AbstractRecord
同名的成员
如果所有的AbstractRecord
都有标题和相似性
,那么在AbstractRecord
中实现getTitle()
和getSimilarity()
会更有意义,您的标题
和相似性
记录类的成员隐藏与抽象记录
同名的成员,而不是在记录中
如果所有的AbstractRecord
都有标题和相似性
,那么在AbstractRecord
中实现getTitle()
和getSimilarity()
将更有意义,而不是在Record
中,基类的两个字段都将继承到子类。您只需使用super
关键字即可访问基类的字段,如下所示:
super.similarity
和super.title
基类的两个字段都将继承给子类。您只需使用super
关键字即可访问基类的字段,如下所示:
super.similarity
和super.title
首先,您必须初始化父类(AbstractRecord)变量
它可以在父类构造函数中完成,也可以在子类中通过super关键字完成
super.title = "value";
这个关键字也可以用来访问它
public String getParentTitle(){
return super.title;
}
首先,必须初始化父类(AbstractRecord)变量
它可以在父类构造函数中完成,也可以在子类中通过super关键字完成
super.title = "value";
这个关键字也可以用来访问它
public String getParentTitle(){
return super.title;
}
您不应该在子类中重新定义标题
和相似性
属性,在定义实际属性的类中创建抽象
getter似乎过于复杂。然后根据经验,类中的属性应该在类中初始化
子类继承所有public
和protected
成员(方法和属性)。这意味着您可以使用它们,而无需重新声明
在下面代码的简化版本中,我一直在子类构造函数中检查参数,假设您可以拥有具有不同约束的不同子类。请注意,如果记录的比较仅在标题
和相似性
上进行,则可以在抽象记录
中实现
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
}
公共抽象类AbstractRecord{
受保护的字符串标题;
保护整数相似性;
受保护的抽象记录(字符串标题、整数相似性){
this.title=标题;
相似性=相似性;
}
public int getSimilarity(){return similarity;}
公共字符串getTitle(){return title;}
公共字符串toString(){
返回“[”+this.title+”:“+this.similarity+””;
}
//一些抽象方法
}
公共类记录扩展AbstractRecord实现可比较{
公共记录(字符串标题、整数相似性)引发IndexException{
超级(标题、相似性);
if(title==null | | title.isEmpty()| |相似度<0){
抛出新的IndexException(“缺少标题”);
}
}
@凌驾
公共整数比较(记录r){
返回0;
}
//AbstractRecord中抽象方法的实现
}
您不应该在子类中重新定义标题
和相似性
属性,在定义实际属性的类中创建抽象
getter似乎过于复杂。然后根据经验,类中的属性应该在类中初始化
子类i