如果此枚舉對象與定義的對象相同,則 Enum 類的 equals() 方法返回 true。
用法
public final boolean equals(Object other)
覆蓋
Enum 類的 equals() 方法覆蓋了 Object 類的 equals() 方法。
參數
other- 它是要與此枚舉進行比較的對象。
返回值
如果定義的對象等於此枚舉,則 equals() 方法返回 true。
例子1
public class Enum_equalsMethodExample1 {
enum Colour{
red,reD;
}
public static void main(String[] args) {
Colour red,reD;
red=Colour.red;
reD=Colour.reD;
if(red.equals(reD)){
System.out.println("Both are equal");
}
else{
System.out.println("red and reD are not equal because of case sensitivity");
}
}
}
輸出:
red and reD are not equal because of case sensitivity
例子2
import java.util.Scanner;
public class Enum_equalsMethodExample2 {
enum SPS{
Stone,Paper,Scissor;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Choose amongst the following:");
for(SPS game:SPS.values()){
System.out.println(game);
}
System.out.print("# ");String str = scanner.next();
SPS Stone;
SPS Paper;
String Scissor;
Scissor =SPS.Scissor.toString();
Stone =SPS.Stone;
Paper=SPS.Paper;
if(Paper.toString().equals(str)) {
System.out.println("There is a Tie");
}
else if(str.equals(Stone.toString())){
System.out.println("Sorry ! You lose.");
}
else if(str.equals(Scissor)){
System.out.println("Hurray! Yor won.");
}
}
}
輸出:
Choose amongst the following: Stone Paper Scissor #
例子3
public class Enum_equalsMethodExample3 {
enum movie{
Don,DDlJ,Fan,Zero;
}
public static void main(String[] args) {
movie Don,DDLJ,Fan,Zero;
Don=movie.Don;
System.out.println("Q In which movie Shah rukh khan has played a dual role ?");
String obj="Don";
Boolean b1 =obj.equals(Don.toString());
if(b1==true){
System.out.println("Ans. "+obj);
System.out.println("Your answer is correct.");
}
else{
System.out.println("Sorry!You Failed.\n The Correct answer is Don.");
}
}
}
輸出:
Q In which movie Shah rukh khan has played a dual role ? Ans. Don Your answer is correct.
相關用法
- Java Enum equals()用法及代碼示例
- Java Enum toString()用法及代碼示例
- Java Enum ordinal()用法及代碼示例
- Java Enum valueOf()用法及代碼示例
- Java Enum hashCode()用法及代碼示例
- Java Enum finalize()用法及代碼示例
- Java Enum clone()用法及代碼示例
- Java Enum name()用法及代碼示例
- Java Enum getDeclaringClass()用法及代碼示例
- Java Enum compareTo()用法及代碼示例
- Java EnumSet range()用法及代碼示例
- Java EnumMap remove()用法及代碼示例
- Java EnumMap hashCode()用法及代碼示例
- Java EnumMap get()用法及代碼示例
- Java EnumSet complementOf()用法及代碼示例
- Java EnumSet clone()用法及代碼示例
- Java EnumMap entrySet()用法及代碼示例
- Java EnumMap containsKey()用法及代碼示例
- Java EnumMap containsValue(value)用法及代碼示例
- Java Enumeration asIterator()用法及代碼示例
注:本文由純淨天空篩選整理自 Java Enum equals() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。