我有一个不可变对象,其权重为 int,在我的代码中,我需要更新权重,inroder 这样做,我需要制作该对象的副本并使用更新的值设置权重。但是该对象没有 clone() 覆盖,我不知道哪种方式更好,clone() 还是实现 Cloneable 接口?
这是我的课:
public class WeightedEdge implements Edge {
private final WeightedEdgeComparator _weightedEdgeComparator;
private final Vertex _target;
private final int _weight;
WeightedEdge(Bundle bundle, Vertex target, int weight) {
_weightedEdgeComparator = new EdgeComparator(bundle.getDirection());
_target = target;
_weight = weight;
}
@Override
public Vertex target() {
return _target;
}
@Override
public int weight() {
return _weight;
}
@Override
public int compareTo(WeightedEdge o) {
return _EdgeComparator.compare(this, o);
}
@Override
public int hashCode() {...}
@Override
public boolean equals(Object obj) { ... }
@Override
public String toString() { ... }