java enum方法_Java Enum compareTo()方法与示例

java enum方法

枚举类compareTo()方法 (Enum Class compareTo() method)

  • compareTo() method is available in java.lang package.

    compareTo()方法在java.lang包中可用。

  • compareTo() method is used to check equality or inequality for this Enum object against the given Enum object mathematically or in other words, we can say this method is used to compare two Enum constants of the same type.

    compareTo()方法用于在数学上检查给定Enum对象与此给定Enum对象的相等性或不相等性,换句话说,可以说此方法用于比较两个相同类型的Enum常量。

  • compareTo() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

    compareTo()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。

  • compareTo() method is a final method so, it is not overridable in child class.

    compareTo()方法是最终方法,因此在子类中不可重写。

  • compareTo() method does not throw an exception at the time of comparing the Enum object.

    比较Enum对象时, compareTo()方法不会引发异常。

    NullPointerException: When the specified argument is null.

    NullPointerException :当指定的参数为null时 。

Syntax:

句法:

    public final int compareTo(Enum obj2);

Parameter(s):

参数:

  • Enum obj2 – represents the Enum object to compare with.

    枚举obj2 –表示要比较的枚举对象。

Return value:

返回值:

The return type of this method is int, it returns an integer value based on the given cases,

此方法的返回类型为int ,它基于给定的情况返回一个整数值,

  • It returns 0 when this Enum object is equal to or same as the given Enum object.

    当此Enum对象等于或等于给定的Enum对象时,它将返回0

  • It returns positive value when this Enum object is greater than the given Enum object.

    当此Enum对象大于给定的Enum对象时,它将返回正值

  • It returns negative value when this Enum object is less than the given Enum object.

    当此Enum对象小于给定的Enum对象时,它将返回负值

Example:

例:

// Java program to demonstrate the example 
// of int compareTo(Enum obj2) method of 
// Enum class

enum Weeks {

    SUN,
    MON,
    TUE,
    WED,
    THU,
    FRI,
    SAT;
}
public class CompareTo {
    public static void main(String args[]) {

        Weeks w1 = Weeks.SUN;
        Weeks w2 = Weeks.MON;
        Weeks w3 = Weeks.TUE;
        Weeks w4 = Weeks.WED;
        Weeks w5 = Weeks.THU;
        Weeks w6 = Weeks.FRI;
        Weeks w7 = Weeks.SAT;

        int res1 = w1.compareTo(w2);
        int res2 = w2.compareTo(w3);
        int res3 = w3.compareTo(w4);
        int res4 = w4.compareTo(w2);
        int res5 = w5.compareTo(w6);

        System.out.println("Is" + " " + w1.name() + " " + "same as" + " " + w2.name() + res1);
        System.out.println("Is" + " " + w2.name() + " " + "same as" + " " + w3.name() + res2);
        System.out.println("Is" + " " + w3.name() + " " + "same as" + " " + w4.name() + res3);
        System.out.println("Is" + " " + w4.name() + " " + "same as" + " " + w2.name() + res4);
        System.out.println("Is" + " " + w5.name() + " " + "same as" + " " + w6.name() + res5);
    }

}

Output

输出量

Is SUN same as MON-1
Is MON same as TUE-1
Is TUE same as WED-1
Is WED same as MON2
Is THU same as FRI-1


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

java enum方法

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值