【问题标题】:Abstract Class Java抽象类 Java
【发布时间】:2018-10-16 19:51:22
【问题描述】:

我需要写抽象类,看起来像这样。

public abstract class Value {

public abstract String toString();
public abstract Value add(Value v);
public abstract Value sub(Value v);
public abstract boolean eq(Value v);
public abstract boolean lte(Value v);
public abstract boolean gte(Value v);
public abstract boolean neq(Value v);
public abstract boolean equals(Object other);
public abstract int hashCode();
public abstract Value create(String s);

}

现在我需要创建几个继承自该类的类。我从 Int 类开始并像这样实现它:

public class Int extends Value {

int val;

public String toString() {
    String toStr = Integer.toString(val);
    return toStr;
}

public Int add(Value v) {
    Int result = new Int();
   if(v instanceof Int) {
        Int temp = (Int) v;
        result.val = val + temp.val;
    }

    return result;
}

public Int sub(Value v) {
    Int result = new Int();
    if(v instanceof Int) {
        Int temp = (Int) v;
        result.val = val - temp.val;
    }
    return result;
}

public boolean eq(Value o) {
    if(this == o) return true;
    if(this == null) return false;
    if(getClass() != o.getClass()) return false;
    Int other = (Int) o;
    return toString() == other.toString();
}

public boolean lte(Value v) {
    if(v instanceof Int) {
        Int temp = (Int) v;
        return this.val < temp.val;
    }
    return false;
}

public boolean gte(Value v) {
    if(v instanceof Int) { 
        Int temp = (Int) v;
        return this.val > temp.val;
    }
    return false;
}

public boolean neq(Value v) {
    if(v instanceof Int) {
        Int temp = (Int) v;
        return !eq(temp);
    }
    return true;
}

public boolean equals(Object o) {
    if(this == o) return true;
    if(this == null) return false;
    if(getClass() != o.getClass()) return false;
    Int other = (Int) o;
    return toString() == other.toString();
}

public int hashCode() {
    Integer hash = val;
    return hash.hashCode();
}

public Int create(String s) {
    val = Integer.parseInt(s);
    return this;
}

}

一切都在编译和工作,但我不知道我的 hashcode() 函数和 equals() 是否良好。此外,我想使用 create() 来制作这样的对象:

getInstance().create("1234");

我的方法也够用吗?

【问题讨论】:

  • 所以你允许添加Value,但你什么都不做,它不是Int——有点错。使其通用,以便 int 只允许整数。
  • 正在编译?方法add 中的result 是什么?
  • equals() 应该比较状态(不是 toString()):return val == other.val
  • add 返回 this.val + v.val。一切正常。
  • Value 是抽象的,但没有状态,也不为任何方法提供任何实现 - 为什么不将其设为接口?

标签: java class equals abstract hashcode


【解决方案1】:

一切都在编译和工作,但我不知道我的 hashcode() 函数和 equals() 是否良好。

您的 equals() 应该比较 int val 而不是比较对象 (this.val == other.val) 的 toString() 的结果。

您的hashCode() 看起来不错,但我会在其中添加@Override(与equals() 相同)。

此外,我想使用 create() 来制作这样的对象:getInstance().create("1234");

看看它的实现,它看起来不错(即可以根据您的需要工作):

public Int create(String s) {
    val = Integer.parseInt(s);
    return this;
}

虽然我不认为你真的想将它与getInstance() 一起使用。只需Int.create() 就足够了:

public static Int create(String s) {
    val = Integer.parseInt(s);
    return new Int(val);
}

请注意,您需要一个私有构造函数。

另外,正如 cmets 中有人指出的那样,考虑使用泛型而不是继承。

【讨论】:

    【解决方案2】:

    hashCode() 方法很好(虽然我会添加一个@Override 注释,只是为了使代码更易于维护并避免错误),但equals(Object) 绝对不是。

    按照您已有的逻辑,== 不是比较字符串的正确方法。您应该改用 equals(参见,例如,How do I compare strings in Java?)。此外,正如 Joakim Danielson 在 cmets 中指出的那样,this 永远不能是 null - 您应该检查 o 是否是 null

    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null) {
            return false;
        }
        if(getClass() != o.getClass()) {
            return false;
        }
        Int other = (Int) o;
        return toString().equals(other.toString()); // Here!
    }
    

    但平心而论,没有理由使用toString - 你可以只比较内部的val

    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null) {
            return false;
        }
        if(getClass() != o.getClass()) {
            return false;
        }
        Int other = (Int) o;
        return val ==  other.val; // Here!
    }
    

    【讨论】:

    • 再说一次,为什么要检查this 是否为空?
    • @JoakimDanielson 不错,感谢您的关注。从 OP 复制和窃听 :-) 我已经编辑了我的答案来解决这个问题。
    【解决方案3】:

    首先,当您覆盖方法时,请使用@Override Annotation。然后我会以另一种方式实现你的 equals 方法。只需返回 this.val == other.val 而不是 this.toString() == other.toString()。你的 toString() 方法实现没问题。你的 hashCode 也很好。但请删除该创建方法。请改用构造函数。

    【讨论】:

    • 谢谢,我会添加注释。我需要做那个创建功能,但不知道怎么做。它是我家庭作业的一部分。稍后我必须创建 ArrayList 并创建特定的值类型
    • @GrzegorzKunc 该方法应该做什么?
    【解决方案4】:

    我可以像这样使用 eq() 实现 equals() 方法吗?

    public boolean equals(Object o) {
        Value compare = (Value) o;
        return eq(compare);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-06
      • 2017-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多