当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java CompoundName clone()用法及代码示例


javax.naming.CompoundName类的clone()方法用于返回此复合名称对象的副本。如果您对此原始化合物名称的组成部分进行了任何更改,都不会影响CompoundName对象的新副本,反之亦然。副本和此化合物名称共享相同的语法。

用法:

public Object clone()

参数:此方法不接受任何内容。



返回值:此方法返回此复合名称的非空副本。

以下示例程序旨在说明CompoundName.clone()方法:
程序1:

// 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<String> enumeration 
            = cloneCompound.getAll(); 
        while (enumeration.hasMoreElements()) 
            System.out.print( 
                enumeration.nextElement() + " "); 
    } 
}
输出:
Clone CompoundName:x|y
Members:
x y

程序2:

// 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<String> 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()




相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 CompoundName clone() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。