Skip to content

Commit a6173f4

Browse files
committed
第二十章 泛型 翻译更新 构建复杂模型
1 parent 74979b1 commit a6173f4

File tree

1 file changed

+138
-1
lines changed

1 file changed

+138
-1
lines changed

docs/book/20-Generics.md

Lines changed: 138 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1086,8 +1086,145 @@ Serializable]
10861086
在[第十二章 集合](./12-Collections.md)的[本章小结](./12-Collections.md#本章小结)部分将会用到这里的输出结果。
10871087

10881088
<!-- Building Complex Models -->
1089-
## 复杂模型构建
1089+
## 构建复杂模型
10901090

1091+
泛型的一个重要好处是能够简单安全地创建复杂模型。例如,我们可以地轻松创建一个元组列表:
1092+
1093+
```java
1094+
// generics/TupleList.java
1095+
// Combining generic types to make complex generic types
1096+
1097+
import onjava.Tuple4;
1098+
1099+
import java.util.ArrayList;
1100+
1101+
public class TupleList<A, B, C, D>
1102+
extends ArrayList<Tuple4<A, B, C, D>> {
1103+
public static void main(String[] args) {
1104+
TupleList<Vehicle, Amphibian, String, Integer> tl =
1105+
new TupleList<>();
1106+
tl.add(TupleTest2.h());
1107+
tl.add(TupleTest2.h());
1108+
tl.forEach(System.out::println);
1109+
}
1110+
}
1111+
/* Output:
1112+
(Vehicle@7cca494b, Amphibian@7ba4f24f, hi, 47)
1113+
(Vehicle@3b9a45b3, Amphibian@7699a589, hi, 47)
1114+
*/
1115+
```
1116+
1117+
这将产生一个功能强大的数据结构,而无需太多代码。
1118+
1119+
下面是第二个例子。每个类都是组成块,总体包含很多个块。在这里,该模型是一个具有过道,货架和产品的零售商店:
1120+
1121+
```java
1122+
// generics/Store.java
1123+
// Building a complex model using generic collections
1124+
1125+
import onjava.Suppliers;
1126+
1127+
import java.util.ArrayList;
1128+
import java.util.Random;
1129+
import java.util.function.Supplier;
1130+
1131+
class Product {
1132+
private final int id;
1133+
private String description;
1134+
private double price;
1135+
1136+
Product(int idNumber, String descr, double price) {
1137+
id = idNumber;
1138+
description = descr;
1139+
this.price = price;
1140+
System.out.println(toString());
1141+
}
1142+
1143+
@Override
1144+
public String toString() {
1145+
return id + ": " + description +
1146+
", price: $" + price;
1147+
}
1148+
1149+
public void priceChange(double change) {
1150+
price += change;
1151+
}
1152+
1153+
public static Supplier<Product> generator =
1154+
new Supplier<Product>() {
1155+
private Random rand = new Random(47);
1156+
1157+
@Override
1158+
public Product get() {
1159+
return new Product(rand.nextInt(1000), "Test",
1160+
Math.round(
1161+
rand.nextDouble() * 1000.0) + 0.99);
1162+
}
1163+
};
1164+
}
1165+
1166+
class Shelf extends ArrayList<Product> {
1167+
Shelf(int nProducts) {
1168+
Suppliers.fill(this, Product.generator, nProducts);
1169+
}
1170+
}
1171+
1172+
class Aisle extends ArrayList<Shelf> {
1173+
Aisle(int nShelves, int nProducts) {
1174+
for (int i = 0; i < nShelves; i++)
1175+
add(new Shelf(nProducts));
1176+
}
1177+
}
1178+
1179+
class CheckoutStand {
1180+
}
1181+
1182+
class Office {
1183+
}
1184+
1185+
public class Store extends ArrayList<Aisle> {
1186+
private ArrayList<CheckoutStand> checkouts =
1187+
new ArrayList<>();
1188+
private Office office = new Office();
1189+
1190+
public Store(
1191+
int nAisles, int nShelves, int nProducts) {
1192+
for (int i = 0; i < nAisles; i++)
1193+
add(new Aisle(nShelves, nProducts));
1194+
}
1195+
1196+
@Override
1197+
public String toString() {
1198+
StringBuilder result = new StringBuilder();
1199+
for (Aisle a : this)
1200+
for (Shelf s : a)
1201+
for (Product p : s) {
1202+
result.append(p);
1203+
result.append("\n");
1204+
}
1205+
return result.toString();
1206+
}
1207+
1208+
public static void main(String[] args) {
1209+
System.out.println(new Store(5, 4, 3));
1210+
}
1211+
}
1212+
/* Output: (First 8 Lines)
1213+
258: Test, price: $400.99
1214+
861: Test, price: $160.99
1215+
868: Test, price: $417.99
1216+
207: Test, price: $268.99
1217+
551: Test, price: $114.99
1218+
278: Test, price: $804.99
1219+
520: Test, price: $554.99
1220+
140: Test, price: $530.99
1221+
...
1222+
*/
1223+
```
1224+
1225+
`Store.toString()` 显示了结果:尽管有复杂的层次结构,但多层的集合仍然是类型安全的和可管理的。令人印象深刻的是,组装这样的模型在理论上并不是禁止的。
1226+
1227+
**Shelf** 使用 `Suppliers.fill()` 这个实用程序,该实用程序接受 **Collection** (第一个参数),并使用 **Supplier** (第二个参数),以元素的数量为 **n** (第三个参数)来填充它。 **Suppliers** 类将会在本章末尾定义,其中的方法都是在执行某种填充操作,并在本章的其他示例中使用。
10911228

10921229
<!-- The Mystery of Erasure -->
10931230
## 泛型擦除

0 commit comments

Comments
 (0)