Adapter example2

package com.eric.interfaceAndInnerClass;

import java.io.IOException;
import java.nio.CharBuffer;
import java.util.Random;
import java.util.Scanner;

/**Scanner 的构造器需要接受一个实现了Readable接口的类作为构造参数,为了让RandomDouble 可以被Scanner类所处理,所以RandomDouble 实现了Readable接口

这样就可以通过Scaner去处理RandomDouble了*/

public class ScannerRandomDouble { public static void main(String[] args) { Scanner scan = new Scanner(new RandomDouble(10)); while (scan.hasNext()) { System.out.println(scan.next()); } } } class RandomDouble implements Readable { private static Random rand = new Random(47); private static final char[] capitals = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(); private static final char[] lowers = "abcdefghijklmnopqrstuvwxyz".toCharArray(); private static final char[] vowels = "aeiou".toCharArray(); private int count; public RandomDouble(int count) { this.count = count; } public int read(CharBuffer cb) throws IOException { if (count-- <= 0) return -1; for (int i = 0; i < 4; i++) { cb.append(capitals[rand.nextInt(capitals.length)]); cb.append(lowers[rand.nextInt(lowers.length)]); } cb.append(vowels[rand.nextInt(vowels.length)]); cb.append('\n'); return 9; } }

 

 

 
 


你可能感兴趣的:(String,Random,Class)