當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。