私はJavaを学んでおり、読んでいる本にはクローン作成に関する次の例があります。ではclone()
、バッファが。であっても、最初のインスタンスは新しいオブジェクトにバッファを設定できますprivate
。これが機能するためには、フィールドが必要なようですprotected
。
なぜこれが許可されるのですか?フィールドclone()
へのアクセスを許可する特別な特権がありますか?private
public class IntegerStack implements Cloneable {
private int[] buffer;
private int top;
// ... code omitted ...
@Override
public IntegerStack clone() {
try{
IntegerStack nObj = (IntegerStack) super.clone();
nObj.buffer = buffer.clone();
return nObj;
} catch (CloneNotSupportedException e)
{
throw new InternalError(e.toString());
}
}
}