Skip to content

Commit fdb6abf

Browse files
committed
������ϰ21
1 parent 168db54 commit fdb6abf

2 files changed

Lines changed: 120 additions & 64 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package generics;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import static net.mindview.util.Print.*;
6+
7+
//: generics/ClassTypeCapture.java
8+
class ClassTypeCapture2 {
9+
Map<String, Class<?>> types = new HashMap<String, Class<?>>();
10+
public Object createNew(String typename) {
11+
Class<?> cl = types.get(typename);
12+
try {
13+
return cl.newInstance();
14+
} catch (NullPointerException e) {
15+
// TODO Auto-generated catch block
16+
// e.printStackTrace();
17+
print("Not a registered typename: "+typename);
18+
} catch (IllegalAccessException e) {
19+
// TODO Auto-generated catch block
20+
e.printStackTrace();
21+
} catch (InstantiationException e) {
22+
// TODO Auto-generated catch block
23+
e.printStackTrace();
24+
}
25+
return null;
26+
}
27+
public void addType(String typename, Class<?> kind) {
28+
types.put(typename, kind);
29+
}
30+
}
31+
public class E21_ClassTypeCapture<T> {
32+
public static void main(String[] args) {
33+
ClassTypeCapture2 ctt = new ClassTypeCapture2();
34+
ctt.addType("Building", Building.class);
35+
ctt.addType("House", House.class);
36+
ctt.addType("Product", Product.class);
37+
print(ctt.createNew("Building").getClass());
38+
print(ctt.createNew("House").getClass());
39+
// Product类没有无参构造器
40+
ctt.createNew("Product");
41+
ctt.createNew("Car");
42+
}
43+
} /* Output:
44+
class generics.Building
45+
class generics.House
46+
java.lang.InstantiationException: generics.Product
47+
at java.lang.Class.newInstance(Class.java:364)
48+
at generics.ClassTypeCapture2.createNew(E21_ClassTypeCapture.java:13)
49+
at generics.E21_ClassTypeCapture.main(E21_ClassTypeCapture.java:39)
50+
Not a registered typename: Car
51+
*///:~

‎src/generics/Store.java‎

Lines changed: 69 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,84 @@
1+
package generics;
2+
13
//: generics/Store.java
24
// Building up a complex model using generic containers.
35
import java.util.*;
46
import net.mindview.util.*;
57

68
class Product {
7-
private final int id;
8-
private String description;
9-
private double price;
10-
public Product(int IDnumber, String descr, double price){
11-
id = IDnumber;
12-
description = descr;
13-
this.price = price;
14-
System.out.println(toString());
15-
}
16-
public String toString() {
17-
return id + ": " + description + ", price: $" + price;
18-
}
19-
public void priceChange(double change) {
20-
price += change;
21-
}
22-
public static Generator<Product> generator =
23-
new Generator<Product>() {
24-
private Random rand = new Random(47);
25-
public Product next() {
26-
return new Product(rand.nextInt(1000), "Test",
27-
Math.round(rand.nextDouble() * 1000.0) + 0.99);
28-
}
29-
};
9+
private final int id;
10+
private String description;
11+
private double price;
12+
13+
public Product(int IDnumber, String descr, double price) {
14+
id = IDnumber;
15+
description = descr;
16+
this.price = price;
17+
System.out.println(toString());
18+
}
19+
20+
public String toString() {
21+
return id + ": " + description + ", price: $" + price;
22+
}
23+
24+
public void priceChange(double change) {
25+
price += change;
26+
}
27+
28+
public static Generator<Product> generator = new Generator<Product>() {
29+
private Random rand = new Random(47);
30+
31+
public Product next() {
32+
return new Product(rand.nextInt(1000), "Test", Math.round(rand
33+
.nextDouble() * 1000.0) + 0.99);
34+
}
35+
};
3036
}
3137

3238
class Shelf extends ArrayList<Product> {
33-
public Shelf(int nProducts) {
34-
Generators.fill(this, Product.generator, nProducts);
35-
}
36-
}
39+
public Shelf(int nProducts) {
40+
Generators.fill(this, Product.generator, nProducts);
41+
}
42+
}
3743

3844
class Aisle extends ArrayList<Shelf> {
39-
public Aisle(int nShelves, int nProducts) {
40-
for(int i = 0; i < nShelves; i++)
41-
add(new Shelf(nProducts));
42-
}
45+
public Aisle(int nShelves, int nProducts) {
46+
for (int i = 0; i < nShelves; i++)
47+
add(new Shelf(nProducts));
48+
}
4349
}
4450

45-
class CheckoutStand {}
46-
class Office {}
51+
class CheckoutStand {
52+
}
53+
54+
class Office {
55+
}
4756

4857
public class Store extends ArrayList<Aisle> {
49-
private ArrayList<CheckoutStand> checkouts =
50-
new ArrayList<CheckoutStand>();
51-
private Office office = new Office();
52-
public Store(int nAisles, int nShelves, int nProducts) {
53-
for(int i = 0; i < nAisles; i++)
54-
add(new Aisle(nShelves, nProducts));
55-
}
56-
public String toString() {
57-
StringBuilder result = new StringBuilder();
58-
for(Aisle a : this)
59-
for(Shelf s : a)
60-
for(Product p : s) {
61-
result.append(p);
62-
result.append("\n");
63-
}
64-
return result.toString();
65-
}
66-
public static void main(String[] args) {
67-
System.out.println(new Store(14, 5, 10));
68-
}
69-
} /* Output:
70-
258: Test, price: $400.99
71-
861: Test, price: $160.99
72-
868: Test, price: $417.99
73-
207: Test, price: $268.99
74-
551: Test, price: $114.99
75-
278: Test, price: $804.99
76-
520: Test, price: $554.99
77-
140: Test, price: $530.99
78-
...
79-
*///:~
58+
private ArrayList<CheckoutStand> checkouts = new ArrayList<CheckoutStand>();
59+
private Office office = new Office();
60+
61+
public Store(int nAisles, int nShelves, int nProducts) {
62+
for (int i = 0; i < nAisles; i++)
63+
add(new Aisle(nShelves, nProducts));
64+
}
65+
66+
public String toString() {
67+
StringBuilder result = new StringBuilder();
68+
for (Aisle a : this)
69+
for (Shelf s : a)
70+
for (Product p : s) {
71+
result.append(p);
72+
result.append("\n");
73+
}
74+
return result.toString();
75+
}
76+
77+
public static void main(String[] args) {
78+
System.out.println(new Store(14, 5, 10));
79+
}
80+
} /*
81+
* Output: 258: Test, price: $400.99 861: Test, price: $160.99 868: Test, price:
82+
* $417.99 207: Test, price: $268.99 551: Test, price: $114.99 278: Test, price:
83+
* $804.99 520: Test, price: $554.99 140: Test, price: $530.99 ...
84+
*/// :~

0 commit comments

Comments
 (0)