mirror of
https://github.com/Snailclimb/JavaGuide
synced 2025-06-16 18:10:13 +08:00
format code
This commit is contained in:
parent
814bda6bf7
commit
a3baf5386f
@ -51,10 +51,7 @@ public class Pizza {
|
||||
}
|
||||
|
||||
public boolean isDeliverable() {
|
||||
if (getStatus() == PizzaStatus.READY) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
return getStatus() == PizzaStatus.READY;
|
||||
}
|
||||
|
||||
// Methods that set and get the status variable.
|
||||
@ -63,9 +60,9 @@ public class Pizza {
|
||||
|
||||
## 3.使用 == 比较枚举类型
|
||||
|
||||
由于枚举类型确保JVM中仅存在一个常量实例,因此我们可以安全地使用“ ==”运算符比较两个变量,如上例所示;此外,“ ==”运算符可提供编译时和运行时的安全性。
|
||||
由于枚举类型确保JVM中仅存在一个常量实例,因此我们可以安全地使用 `==` 运算符比较两个变量,如上例所示;此外,`==` 运算符可提供编译时和运行时的安全性。
|
||||
|
||||
首先,让我们看一下以下代码段中的运行时安全性,其中“ ==”运算符用于比较状态,并且如果两个值均为null 都不会引发 NullPointerException。相反,如果使用equals方法,将抛出 NullPointerException:
|
||||
首先,让我们看一下以下代码段中的运行时安全性,其中 `==` 运算符用于比较状态,并且如果两个值均为null 都不会引发 NullPointerException。相反,如果使用equals方法,将抛出 NullPointerException:
|
||||
|
||||
```java
|
||||
if(testPz.getStatus().equals(Pizza.PizzaStatus.DELIVERED));
|
||||
@ -84,9 +81,12 @@ if(testPz.getStatus() == TestColor.GREEN);
|
||||
```java
|
||||
public int getDeliveryTimeInDays() {
|
||||
switch (status) {
|
||||
case ORDERED: return 5;
|
||||
case READY: return 2;
|
||||
case DELIVERED: return 0;
|
||||
case ORDERED:
|
||||
return 5;
|
||||
case READY:
|
||||
return 2;
|
||||
case DELIVERED:
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user