Java文档说:
类实现了Cloneable接口,以向Object.clone()方法指示该方法合法地为该类的实例制作字段的字段副本.
在未实现Cloneable接口的实例上调用Object的clone方法会导致抛出异常CloneNotSupportedException.
按照惯例,实现此接口的类应使用公共方法覆盖Object.clone(受保护).有关重写此方法的详细信息,请参阅Object.clone().
请注意,此接口不包含克隆方法.因此,仅仅通过实现该接口的事实来克隆对象是不可能的.即使反射调用clone方法,也无法保证它会成功.
我有这UserProfile堂课:
public class UserProfile implements Cloneable {
private String name;
private int ssn;
private String address;
public UserProfile(String name, int ssn, String address) {
this.name = name;
this.ssn = ssn;
this.address = address;
}
public UserProfile(UserProfile user) {
this.name = user.getName();
this.ssn = user.getSSN();
this.address = user.getAddress();
}
// get methods here...
@Override
public UserProfile clone() {
return new UserProfile(this);
}
}
Run Code Online (Sandbox Code Playgroud)
为了测试porpuses,我这样做main():
UserProfile up1 = new UserProfile("User", 123, "Street");
UserProfile …Run Code Online (Sandbox Code Playgroud) 在我发布之前,我读了一些以前的帖子,我真的没有看到我的逻辑有什么问题.(我已经花了3个小时了,这可能会让我的快乐时光消失)*我从来不想知道答案,我喜欢努力工作,也许有人可以问我一个关于我想要实现的目标的问题可以让我用你的线索或提示来思考答案.我们将不胜感激.* obj2没有克隆,所以在异常stackTrace之后,我发现在同一行上有一个nullpointer异常,这意味着obj2永远不会被克隆.请帮我思考一点.
package testbankaccount;
/**
*
* @author
*/
public interface Cloneable {
}
Run Code Online (Sandbox Code Playgroud)
我的父班
package testbankaccount;
/**
*
* @author
*/
public abstract class BankAccount implements Cloneable, Comparable {
private double balance;
private int numberofDeposits;
private int numberofWithdrawals;
private double annualInterestRate;
private double monthlyServiceCharges;
private String customerName;
protected BankAccount(){
this(1.0, 1.0);
}
protected BankAccount(double balance, double annualInterestRate){
this.balance = balance;
this.annualInterestRate = annualInterestRate;
}
public void deposit(double deposit){
balance += deposit;
numberofDeposits++;
}
public void withdraw(double …Run Code Online (Sandbox Code Playgroud) 在尝试克隆可变集合时,我最初的方法是在mutable.Cloneable特征上使用clone()方法.但是,这遵循java.Object.clone实现,该实现创建引用的副本,而不是深层副本.从测试中,我可以确认可变.{Queue,Seq,Set}所有浅拷贝.
我已经采用原始的新xxx(copy:_*)方法来创建一个深层副本,但我的问题是mutable.Cloneable特性的目的是什么,如果它没有实现?
我可以使用什么代替可以克隆的"长"?
请参阅下面我在此处收到错误的代码,因为长期不可克隆.
public static CloneableDictionary<string, long> returnValues = new CloneableDictionary<string, long>();
Run Code Online (Sandbox Code Playgroud)
编辑:我忘了提到我想使用我找到的以下代码(见下文).
public class CloneableDictionary<TKey, TValue> : Dictionary<TKey, TValue> where TValue : ICloneable
{
public IDictionary<TKey, TValue> Clone()
{
var clone = new CloneableDictionary<TKey, TValue>();
foreach (KeyValuePair<TKey, TValue> pair in this)
{
clone.Add(pair.Key, (TValue)pair.Value.Clone());
}
return clone;
}
}
Run Code Online (Sandbox Code Playgroud) 这看起来相对简单,我只是难以理解jQuery语法.
基本上我想采用这种形式:
<div class="me_signup">
<input type="text" name="referral[0][name]" id="referral_0_name">
<br>
<input type="text" name="referral[0][email]" id="referral_0_email">
</div>
Run Code Online (Sandbox Code Playgroud)
并用一个按钮复制它并增加变量号..
$(".add_another_button").click(function(){
...
};
Run Code Online (Sandbox Code Playgroud) 我阅读了有效的Java书,并且不了解一个解释Clonable接口的段落.有人可以解释一下这段话:
程序员假设如果他们扩展一个类并
super.clone从子类调用 ,则返回的对象将是子类的一个实例.超类可以提供此功能的唯一方法是返回通过调用获得的对象super.clone.如果clone方法返回由构造函数创建的对象,则它将具有错误的类.
谢谢.
我几乎没有问题/疑点来填充HashMap中的值
我希望HashMap接受"Student"作为键,"Details"作为值.由于hashMap的键应该是不可变的,所以我有一些疑问,如果可以解决这个问题
学生班参考哪个参考"实验室"
public class Student {
private String id;
private String name;
private Department dept;
public Student(String id, String name, Department dept)
{
this.id=id;
this.name=name;
this.dept=dept;
}
public Department getDepartment()
{
return this.dept;
}
}
public class Department {
private String deptId;
private Lab lab;
public Department(String deptId, Lab lab)
{
this.deptId=deptId;
this.lab=lab;
}
public void setLab(Lab lab)
{
this.lab=lab;
}
}
public class Lab {
private String labId;
private String labName;
public Lab(String labId, String labName)
{ …Run Code Online (Sandbox Code Playgroud)一个技术能力问题
HashMap<String, String> map = new HashMap<String,String>();
String key1 = "key1";
map.put(key1, "value1");
String key2 = key1.clone();
map.put(key2, "value2");
Run Code Online (Sandbox Code Playgroud)
地图对象的内容是什么?
我回答为 {key1=value2} 但后来意识到 String 不包含 clone 方法。
我想知道同样的原因。
在这个错误报告中,Doug Lea写道(指的是JDK 5.0的预发布版本):
虽然
CopyOnWriteArraySet声明Cloneable,它没有界定公共clone方法.
但它最终会CopyOnWriteArraySet完全没有实现Cloneable接口!(在Java SE 6,7和8中都是如此)
如何CopyOnWriteArraySet从不同的CopyOnWriteArrayList关于克隆?有没有人想要克隆它?
PS我明白clone()不推荐这CopyOnWriteArraySet是基于CopyOnWriteArrayList内部的.
在未实现 Cloneable 接口的实例上调用 Object 的 clone 方法会导致抛出异常 CloneNotSupportedException。
为什么我有错误
clone() 在 java.lang.Object 中具有保护访问权限
但不是 CloneNotSupportedException 异常?
public class Test
{
public static void main(String[] args)
{
Test2 c1 = new Test2();
Test2 c2 = (Test2) c1.clone(); // error: clone() has protected access in java.lang.Object
}
}
class Test2
{
}
Run Code Online (Sandbox Code Playgroud) 我在想这样的事情:
record Foo() implements Cloneable {
public Foo clone() {...}
}
Run Code Online (Sandbox Code Playgroud)
这是一件好事吗?我们应该避免它以支持未来的枯萎吗?
cloneable ×11
java ×8
clone ×2
c# ×1
collections ×1
compareto ×1
exception ×1
forms ×1
immutability ×1
interface ×1
java-16 ×1
java-record ×1
jquery ×1
long-integer ×1
mutable ×1
scala ×1
string ×1