Java的Locale clone() 方法和示例
Java中Locale 类的clone()方法用于简单地创建现有语言环境的克隆或副本。该方法将一种语言环境的内容复制到另一种语言环境。
句法:
Sec_Locale = First_Locale.clone()
参数:该方法不带任何参数。
返回值:此方法返回此语言环境的克隆。
下面的程序说明了 clone() 方法的工作:
方案一:
// Java code to illustrate clone() method
import java.util.*;
public class Locale_Demo {
public static void main(String[] args)
{
// Creating a new locale
Locale first_locale
= new Locale("no", "NO", "NY");
// Creating a second locale
Locale sec_locale;
// Displaying first locale
System.out.println("First Locale: "
+ first_locale);
// Cloning first locale to second
sec_locale
= (Locale)first_locale.clone();
// Displaying second locale
System.out.println("Second Locale: "
+ sec_locale);
}
}
输出:
First Locale: no_NO_NY
Second Locale: no_NO_NY
方案二:
// Java code to illustrate clone() method
import java.util.*;
public class Locale_Demo {
public static void main(String[] args)
{
// Creating a new locale
Locale first_locale
= new Locale("ga", "IE");
// Creating a second locale
Locale sec_locale;
// Displaying first locale
System.out.println("First Locale: "
+ first_locale);
// Cloning first locale to second
sec_locale
= (Locale)first_locale.clone();
// Displaying second locale
System.out.println("Second Locale: "
+ sec_locale);
}
}
输出:
First Locale: ga_IE
Second Locale: ga_IE