- 论坛徽章:
- 84
|
看 Effective Java 没看懂,看了这篇文章终于懂了
http://ifeve.com/static-factory- ... ional-constructors/
不就是在方法里面调用构造函数么,绕了好半天,,- public class RandomIntGenerator {
- private final int min;
- private final int max;
- private RandomIntGenerator(int min, int max) {
- this.min = min;
- this.max = max;
- }
- public static RandomIntGenerator between(int max, int min) {
- return new RandomIntGenerator(min, max);
- }
- public static RandomIntGenerator biggerThan(int min) {
- return new RandomIntGenerator(min, Integer.MAX_VALUE);
- }
- public static RandomIntGenerator smallerThan(int max) {
- return new RandomIntGenerator(Integer.MIN_VALUE, max);
- }
- public int next() {...}
- }
复制代码 |
|