📜  Java中的 RuleBasedCollator clone() 方法及示例

📅  最后修改于: 2022-05-13 01:54:30.901000             🧑  作者: Mango

Java中的 RuleBasedCollator clone() 方法及示例

Java.text.RuleBasedCollator类的clone()方法用于获取此 Collator 对象的副本。
句法:

public Object clone()

参数:此方法不接受任何参数。
返回值:此方法返回此 Collator 对象的副本。
以下是说明clone()方法的示例:
示例 1:

Java
// Java program to demonstrate
// clone() method
 
import java.text.*;
import java.util.*;
import java.io.*;
 
public class GFG {
    public static void main(String[] argv)
    {
        try {
 
            // Creating and initializing new simple rule
            String simple = "< a < b < c < d";
 
            // Creating and initializing
            // new RuleBasedCollator Object
            RuleBasedCollator col
                = new RuleBasedCollator(simple);
 
            // getting copy of this object
            // using clone() mehtod
            Object obj = col.clone();
 
            // display result
            System.out.println("equivalent object :- "
                               + obj);
        }
 
        catch (ClassCastException e) {
 
            System.out.println("Exception thrown : "
                               + e);
        }
        catch (ParseException e) {
 
            System.out.println("Exception thrown : "
                               + e);
        }
    }
}


Java
// Java program to demonstrate
// clone() method
 
import java.text.*;
import java.util.*;
import java.io.*;
 
public class GFG {
    public static void main(String[] argv)
    {
        try {
 
            // Creating and initializing new simple rule
            String simple = "< a < c & a < b";
 
            // Creating and initializing
            // new RuleBasedCollator Object
            RuleBasedCollator col
                = new RuleBasedCollator(simple);
 
            // getting copy of this object
            // using clone() mehtod
            Object obj = col.clone();
 
            // display result
            System.out.println("equivalent object :- "
                               + obj);
        }
 
        catch (ClassCastException e) {
 
            System.out.println("Exception thrown : "
                               + e);
        }
        catch (ParseException e) {
 
            System.out.println("Exception thrown : "
                               + e);
        }
    }
}


输出:
equivalent object :- java.text.RuleBasedCollator@7033e09a

示例 2:

Java

// Java program to demonstrate
// clone() method
 
import java.text.*;
import java.util.*;
import java.io.*;
 
public class GFG {
    public static void main(String[] argv)
    {
        try {
 
            // Creating and initializing new simple rule
            String simple = "< a < c & a < b";
 
            // Creating and initializing
            // new RuleBasedCollator Object
            RuleBasedCollator col
                = new RuleBasedCollator(simple);
 
            // getting copy of this object
            // using clone() mehtod
            Object obj = col.clone();
 
            // display result
            System.out.println("equivalent object :- "
                               + obj);
        }
 
        catch (ClassCastException e) {
 
            System.out.println("Exception thrown : "
                               + e);
        }
        catch (ParseException e) {
 
            System.out.println("Exception thrown : "
                               + e);
        }
    }
}
输出:
equivalent object :- java.text.RuleBasedCollator@78930901

参考: https://docs.oracle.com/javase/9/docs/api/ Java/text/RuleBasedCollator.html#clone–