Skip to content

Commit 6442b92

Browse files
authored
format code
1 parent 281797d commit 6442b92

File tree

1 file changed

+79
-79
lines changed

1 file changed

+79
-79
lines changed

Diff for: ch06/02_Instance_Tests_and_Casts.md

+79-79
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@
1010
```java
1111
public class Integer extends Number {
1212
private final int value;
13-
public Integer(int value) {
14-
this.value=value;
15-
}
16-
public int intValue() {
17-
return value;
18-
}
19-
public boolean equals(Object o) {
20-
if (o instanceof Integer) {
21-
return value == ((Integer)o).intValue();
22-
} else
23-
return false;
24-
}
25-
...
26-
}
13+
public Integer(int value) {
14+
this.value=value;
15+
}
16+
public int intValue() {
17+
return value;
18+
}
19+
public boolean equals(Object o) {
20+
if (o instanceof Integer) {
21+
return value == ((Integer)o).intValue();
22+
} else
23+
return false;
24+
}
25+
...
26+
}
2727
```
2828

2929
`equals` 方法接受 `Object` 类型的参数,检查对象是否为 `Integer` 类的实例,如果是,则将其转换为 `Integer` 并比较两个整数的值。 此代码可用,因为
@@ -37,19 +37,19 @@ public abstract class AbstractList<E> extends AbstractCollection<E> implements L
3737
public boolean equals(Object o) {
3838
if (o instanceof List<E>) { // compile-time error
3939
Iterator<E> it1 = iterator();
40-
Iterator<E> it2 = ((List<E>)o).iterator(); // unchecked cast
41-
while (it1.hasNext() && it2.hasNext()) {
42-
E e1 = it1.next();
43-
E e2 = it2.next();
44-
if (!(e1 == null ? e2 == null : e1.equals(e2)))
45-
return false;
46-
}
47-
return !it1.hasNext() && !it2.hasNext();
48-
} else
49-
return false;
50-
}
51-
...
52-
}
40+
Iterator<E> it2 = ((List<E>)o).iterator(); // unchecked cast
41+
while (it1.hasNext() && it2.hasNext()) {
42+
E e1 = it1.next();
43+
E e2 = it2.next();
44+
if (!(e1 == null ? e2 == null : e1.equals(e2)))
45+
return false;
46+
}
47+
return !it1.hasNext() && !it2.hasNext();
48+
} else
49+
return false;
50+
}
51+
...
52+
}
5353
```
5454

5555
同样,`equals` 方法接受 `Object` 类型的参数,检查对象是否为 `List<E>` 类型的实例,如果是,则将其转换为 `List<E>` 并比较两个列表中的相应元素。此代
@@ -62,17 +62,17 @@ public abstract class AbstractList<E> extends AbstractCollection<E> implements L
6262
编译上面的代码报告了两个问题,一个是实例测试的错误,另一个是未经检查的演员警告:
6363

6464
```java
65-
% javac -Xlint:unchecked AbstractList.java
66-
AbstractList.java:6: illegal generic type for instanceof
67-
if (!(o instanceof List<E>)) return false; // compile-time error
68-
^
69-
AbstractList.java:8: warning: [unchecked] unchecked cast
70-
found : java.lang.Object
71-
required: List<E>
72-
Iterator<E> it2 = ((List<E>)o).iterator(); // unchecked cast
73-
^
74-
1 error
75-
1 warning
65+
% javac -Xlint:unchecked AbstractList.java
66+
AbstractList.java:6: illegal generic type for instanceof
67+
if (!(o instanceof List<E>)) return false; // compile-time error
68+
^
69+
AbstractList.java:8: warning: [unchecked] unchecked cast
70+
found : java.lang.Object
71+
required: List<E>
72+
Iterator<E> it2 = ((List<E>)o).iterator(); // unchecked cast
73+
^
74+
1 error
75+
1 warning
7676
```
7777

7878
实例检查报告错误,因为没有可能的方法来测试给定对象是否属于类型 `List<E>`。 演员报告未经检查的警告; 它将执行转换,但它不能检查列表元素实际上是否为
@@ -83,22 +83,22 @@ public abstract class AbstractList<E> extends AbstractCollection<E> implements L
8383
```java
8484
import java.util.*;
8585
public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
86-
public boolean equals(Object o) {
87-
if (o instanceof List<?>) {
88-
Iterator<E> it1 = iterator();
89-
Iterator<?> it2 = ((List<?>)o).iterator();
90-
while (it1.hasNext() && it2.hasNext()) {
91-
E e1 = it1.next();
92-
Object e2 = it2.next();
93-
if (!(e1 == null ? e2 == null : e1.equals(e2)))
94-
return false;
95-
}
96-
return !it1.hasNext() && !it2.hasNext();
97-
} else
98-
return false;
99-
}
100-
...
101-
}
86+
public boolean equals(Object o) {
87+
if (o instanceof List<?>) {
88+
Iterator<E> it1 = iterator();
89+
Iterator<?> it2 = ((List<?>)o).iterator();
90+
while (it1.hasNext() && it2.hasNext()) {
91+
E e1 = it1.next();
92+
Object e2 = it2.next();
93+
if (!(e1 == null ? e2 == null : e1.equals(e2)))
94+
return false;
95+
}
96+
return !it1.hasNext() && !it2.hasNext();
97+
} else
98+
return false;
99+
}
100+
...
101+
}
102102
```
103103

104104
除了更改实例测试和强制类型外,第二个迭代器的类型也从 `Iterator<E>` 更改为 `Iterator<?>`,第二个元素的类型从 `E` 更改为 `Object`。 代码类型检查,
@@ -117,11 +117,11 @@ public abstract class AbstractList<E> extends AbstractCollection<E> implements L
117117

118118
```java
119119
public static <T> List<T> asList(Collection<T> c) throws InvalidArgumentException {
120-
if (c instanceof List<?>) {
121-
return (List<T>)c;
122-
} else
123-
throw new InvalidArgumentException("Argument not a list");
124-
}
120+
if (c instanceof List<?>) {
121+
return (List<T>)c;
122+
} else
123+
throw new InvalidArgumentException("Argument not a list");
124+
}
125125
```
126126

127127
编译此代码将成功,不会出现错误或警告。 实例测试没有错误,因为 `List<?>` 是可重用的类型。 由于转换源的类型为 `Collection<T>`,转换不会报告警告,并且
@@ -137,26 +137,26 @@ public static <T> List<T> asList(Collection<T> c) throws InvalidArgumentExceptio
137137

138138
```java
139139
class Promote {
140-
public static List<String> promote(List<Object> objs) {
141-
for (Object o : objs)
142-
if (!(o instanceof String))
143-
throw new ClassCastException();
144-
return (List<String>)(List<?>)objs; // unchecked cast
145-
}
146-
public static void main(String[] args) {
147-
List<Object> objs1 = Arrays.<Object>asList("one","two");
148-
List<Object> objs2 = Arrays.<Object>asList(1,"two");
149-
List<String> strs1 = promote(objs1);
150-
assert (List<?>)strs1 == (List<?>)objs1;
151-
boolean caught = false;
152-
try {
153-
List<String> strs2 = promote(objs2);
154-
} catch (ClassCastException e) {
155-
caught = true;
156-
}
157-
assert caught;
158-
}
159-
}
140+
public static List<String> promote(List<Object> objs) {
141+
for (Object o : objs)
142+
if (!(o instanceof String))
143+
throw new ClassCastException();
144+
return (List<String>)(List<?>)objs; // unchecked cast
145+
}
146+
public static void main(String[] args) {
147+
List<Object> objs1 = Arrays.<Object>asList("one","two");
148+
List<Object> objs2 = Arrays.<Object>asList(1,"two");
149+
List<String> strs1 = promote(objs1);
150+
assert (List<?>)strs1 == (List<?>)objs1;
151+
boolean caught = false;
152+
try {
153+
List<String> strs2 = promote(objs2);
154+
} catch (ClassCastException e) {
155+
caught = true;
156+
}
157+
assert caught;
158+
}
159+
}
160160
```
161161

162162
如果任何对象不是字符串,该方法会在对象列表上抛出循环并抛出类抛出异常。 因此,当方法的最后一行到达时,将对象列表转换为字符串列表是安全的。

0 commit comments

Comments
 (0)