-
Notifications
You must be signed in to change notification settings - Fork 0
/
c11test31.java
47 lines (45 loc) · 1.28 KB
/
c11test31.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// 第11章 练习31
// @author 晴和羽织
import java.util.*;
class RandomShapeGenerator1 implements Iterable<Shape> {
private Random rand = new Random();
private final int quantity;
RandomShapeGenerator1(int quantity) {
this.quantity = quantity;
}
public Iterator<Shape> iterator() {
System.out.println("Here is iterator");
return new Iterator<Shape>() {
private int count;
@Override
public boolean hasNext() {
return count < quantity;
}
@Override
public Shape next() {
count++;
return nextShape();
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
private Shape nextShape() {
switch (rand.nextInt(3)) {
default:
return new Triangle();
case 0:
return new Circle();
case 1:
return new Square();
}
}
}
public class c11test31 {
public static void main(String[] args) {
RandomShapeGenerator1 rsg = new RandomShapeGenerator1(10);
for(Shape shape : rsg)
System.out.println(shape.getClass().getSimpleName());
}
}