【发布时间】:2019-09-26 16:10:16
【问题描述】:
public Object clone() throws CloneNotSupportedException {
DoublyLinkedList<E> other=(DoublyLinkedList<E>) super.clone();
if(size > 0){
other.header=new Node<>(null,null,null);
other.trailer=new Node<>(null,other.header,null);
other.header.setNext(other.trailer);
Node<E> walk=header.getNext();
Node<E> otherWalk=other.header;
while (walk != trailer){
Node<E> newest=new Node<>
(walk.getElement(),otherWalk,otherWalk.getNext());
otherWalk.setNext(newest);
walk=walk.getNext();
otherWalk=otherWalk.getNext();
}
}
return other;
}
我将 clone 方法重写为 public 并在我的代码中使用它。
public Object deepCopy() {
Node<E> walk = header;
while (walk != trailer) {
Node<E> newNode = new Node<E>((E)
(walk.getElement()).clone(), null, null);
......
但它有一个错误提示“clone() 在 java.lang.Object 中具有受保护的访问权限”。
我已经在我的班级中将 clone 方法更改为 public,为什么它仍然受到保护?
【问题讨论】:
-
不明白。您要克隆哪个类?