Java中的 CompoundName clone() 方法及示例
javax.naming.CompoundName 类的clone()方法用于返回此复合名称对象的副本。如果您对此原始复合名称的组件进行任何更改,都不会影响 CompoundName 对象的新副本,反之亦然。克隆和此复合名称共享相同的语法。
句法:
public Object clone()
参数:此方法不接受任何内容。
返回值:此方法返回此复合名称的非空副本。
下面的程序说明了 CompoundName.clone() 方法:
方案一:
// Java program to demonstrate
// CompoundName.clone()
import java.util.Enumeration;
import java.util.Properties;
import javax.naming.CompoundName;
import javax.naming.InvalidNameException;
public class GFG {
public static void main(String[] args)
throws InvalidNameException
{
// need properties for CompoundName
Properties props = new Properties();
props.put("jndi.syntax.separator", "|");
props.put("jndi.syntax.direction",
"left_to_right");
// create CompoundName object
CompoundName CompoundName
= new CompoundName(
"x|y", props);
// create clone object
CompoundName cloneCompound
= (CompoundName)CompoundName
.clone();
// print cloned CompoundName
System.out.println(
"Clone CompoundName: "
+ cloneCompound);
// print members
System.out.println("Members:");
Enumeration enumeration
= cloneCompound.getAll();
while (enumeration.hasMoreElements())
System.out.print(
enumeration.nextElement() + " ");
}
}
输出:
Clone CompoundName: x|y
Members:
x y
方案二:
// Java program to demonstrate
// CompoundName.clone() method
import java.util.Enumeration;
import java.util.Properties;
import javax.naming.CompoundName;
import javax.naming.InvalidNameException;
public class GFG {
public static void main(String[] args)
throws InvalidNameException
{
// need properties for CompoundName
Properties props = new Properties();
props.put("jndi.syntax.separator", ":");
props.put("jndi.syntax.direction",
"left_to_right");
// create CompoundName object
CompoundName CompoundName
= new CompoundName(
"x:y:z:a:m:a:q:e",
props);
// create clone object
CompoundName cloneCompound
= (CompoundName)CompoundName
.clone();
// print cloned CompoundName
System.out.println("Clone CompoundName: "
+ cloneCompound);
// print members
System.out.println("Members:");
Enumeration enumeration
= cloneCompound.getAll();
int i = 1;
while (enumeration.hasMoreElements()) {
System.out.println(i + ":"
+ enumeration.nextElement());
i++;
}
}
}
输出:
Clone CompoundName: x:y:z:a:m:a:q:e
Members:
1:x
2:y
3:z
4:a
5:m
6:a
7:q
8:e
参考资料:https://docs.oracle.com/javase/10/docs/api/javax/naming/CompoundName.html#clone()