Java中的 CompositeName clone() 方法及示例
javax.naming.CompositeName 类的clone()方法用于返回此复合名称对象的副本。如果您对此原始复合名称的组件进行任何更改,都不会影响 CompositeName 对象的新副本,反之亦然反之亦然。
句法:
public Object clone()
参数:此方法不接受任何内容。
返回值:此方法返回此复合名称的非空副本。
下面的程序说明了 CompositeName.clone() 方法:
方案一:
// Java program to demonstrate
// CompositeName.clone()
import javax.naming.CompositeName;
import javax.naming.InvalidNameException;
public class GFG {
public static void main(String[] args)
throws InvalidNameException
{
// create Composite name object
CompositeName compositeName
= new CompositeName("x/y");
// create clone object
CompositeName cloneComposite
= (CompositeName)compositeName.clone();
// print CompositeName
System.out.println("Clone CompositeName: "
+ cloneComposite);
}
}
输出:
Clone CompositeName: x/y
方案二:
// Java program to demonstrate
// CompositeName.clone() method
import java.util.Enumeration;
import javax.naming.CompositeName;
import javax.naming.InvalidNameException;
public class GFG {
public static void main(String[] args)
throws InvalidNameException
{
// create Composite name object
CompositeName compositeName
= new CompositeName("x/y/z/a");
// create clone object
CompositeName cloneComposite
= (CompositeName)compositeName.clone();
// print CompositeName
System.out.println("Clone CompositeName: "
+ cloneComposite);
System.out.print("Members: ");
Enumeration enumeration
= cloneComposite.getAll();
while (enumeration.hasMoreElements())
System.out.print(enumeration.nextElement()
+ ", ");
}
}
输出:
Clone CompositeName: x/y/z/a
Members: x, y, z, a,
参考资料:https://docs.oracle.com/javase/10/docs/api/javax/naming/CompositeName.html#clone()