Java Currency getInstance()方法与示例

货币类getInstance()方法 (Currency Class getInstance() method)

Syntax:

句法:

    public static Currency getInstance(Locale lo);
    public static Currency getInstance(String curr_code);

  • getInstance() method is available in java.util package.

    getInstance()方法在java.util包中可用。

  • getInstance(Locale lo) method is used to get the Currency instance for the specified Locale (lo).

    getInstance(Locale lo)方法用于获取指定的Locale(lo)的Currency实例。

  • getInstance(String curr_code) method is used to get the Currency instance for the specified Currency code (curr_code).

    getInstance(String curr_code)方法用于获取指定货币代码(curr_code)的Currency实例。

  • These methods may throw an exception at the time of returning Currency instance.

    这些方法在返回Currency实例时可能会引发异常。

    • NullPointerException: This exception may throw when the given parameter is null exists.NullPointerException :当给定参数为null时,可能引发此异常。
    • IllegalArgumentException: This exception may throw when ISO 3166 un-support the given parameter.IllegalArgumentException :当ISO 3166不支持给定参数时,可能引发此异常。
  • These are static methods, it is accessible with the class name and if we try to access these methods with the class object then also we will not get an error.

    这些是静态方法,可以使用类名进行访问,如果尝试使用类对象访问这些方法,则也不会出错。

Parameter(s):

参数:

  • In the first case, getInstance(Locale lo),

    第一种情况是getInstance(Locale lo)

    • Locale lo – represents the locale for whose Currency instance is needed.
    • 语言环境lo –表示需要其Currency实例的语言环境。
  • In the second case, getInstance(String curr_code)

    在第二种情况下, getInstance(String curr_code)

    • String curr_code – represent the currency code (curr_code).
    • 字符串curr_code –代表货币代码(curr_code)。

Return value:

返回值:

In both the cases, the return type of the method is Currency,

在这两种情况下,方法的返回类型均为Currency 。

  • getInstance(Locale lo) – returns Currency instance for the given locale (lo).

    getInstance(Locale lo) –返回给定语言环境(lo)的Currency实例。

  • getInstance(String curr_code) – returns Currency instance for the given currency code (curr_code).

    getInstance(String curr_code) –返回给定货币代码(curr_code)的Currency实例。

Example:

例:

// Java program is to demonstrate the example of
// getInstance() method of Currency

import java.util.*;

public class GetInstanceOfCurrency {
    public static void main(String args[]) {
        // Instantiates a currency with INR code
        Currency c1 = Currency.getInstance("INR");

        // Instantiates a currency for the given locale
        Locale lo = Locale.US;
        Currency c2 = Currency.getInstance(lo);

        // By using getInstance(c1) method is to return
        // the Currency instance for the given currency code
        System.out.print("c1.getCurrencyCode(): ");
        System.out.println(c1.getCurrencyCode());

        // By using getSymbol(lo) method is to return
        // the Currency instance for the given locale
        System.out.print("c2.getCurrencyCode(): ");
        System.out.println(c2.getCurrencyCode());
    }
}

Output

输出量

c1.getCurrencyCode(): INR
c2.getCurrencyCode(): USD


翻译自: https://www.includehelp.com/java/currency-getinstance-method-with-example.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值