Java Scanner Readable
通过implements(实现)Readbale interface(接口)的 read() method(方法) 实现自己添加字符到buffer里,然后读取
//策略模式
package object; //: interfaces/RandomWords.java // Implementing an interface to conform to a method. import java.nio.*;//Readable 接口在java.nio里面
import java.util.*; public class RandomWords 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 RandomWords(int count) {
this.count = count;
}
public int read(CharBuffer cb) {
//read()方法来自Readable接口,Sanner()里也有用到
if(count-- == 0) return -1; // Indicates end of input
cb.append(capitals[rand.nextInt(capitals.length)]); //从cpitals里面每次读出一个字符并添加到buffer里面
for(int i = 0; i < 4; i++) {
cb.append(vowels[rand.nextInt(vowels.length)]);//从vowels里面每次读出four char 字符并添加到buffer里面
cb.append(lowers[rand.nextInt(lowers.length)]);//append()方法:添加字符到buffer里面
}
cb.append(" "); //在这里空格和'\n'功能一样
return 10; // Number of characters appended
}
public static void main(String[] args) {
Scanner s = new Scanner(new RandomWords(10));
// new RandomWords(10)可以换做任意实现了Readable接口的类所生成的对象
while(s.hasNext()) //hasNext()通过调用readInput()方法调用read()方法知道read()返回-1结束
System.out.println(s.next()); } } /* Output: Yazeruyac Fowenucor Goeazimom Raeuuacio Nuoadesiw Hageaikux Ruqicibui Numasetih Kuuuuozog Waqizeyoy *///:~
我们再次使用了适配器模式,但在本例中,被适配的类可以通过继承和实现Readable接口来创建,因此通过使用interface关键字提供的伪多重继承机制,我们可以生产既是RandomDoubles又是Readable的新类,在这种方式中,我们可以在任何现有类之上添加新的接口,所以这意味着让方法接受接口类型,是一种让任何类都可以对该方法进行适配的方式
//: interfaces/RandomDoubles.java package object; import java.util.*; public class RandomDoubles { private static Random rand = new Random(47); public double next() { return rand.nextDouble(); } public static void main(String[] args) { RandomDoubles rd = new RandomDoubles(); for(int i = 0; i < 7; i ++) System.out.print(rd.next() + " "); } } /* Output: 0.7271157860730044 0.5309454508634242 0.16020656493302599 0.18847866977771732 0.5166020801268457 0.2678662084200585 0.2613610344283964 *///:~
//: interfaces/AdaptedRandomDoubles.java
// Creating an adapter with inheritance.
package object;
import java.nio.*;
import java.util.*;
import object.*;
public class AdaptedRandomDoubles extends RandomDoubles
implements Readable {
private int count;
public AdaptedRandomDoubles(int count) {
this.count = count;
}
public int read(CharBuffer cb) {
if(count-- == 0)
return -1;
String result = Double.toString(next()) + " ";
cb.append(result);
return result.length();
}
public static void main(String[] args) {
Scanner s = new Scanner(new AdaptedRandomDoubles(7));
while(s.hasNextDouble())
System.out.print(s.nextDouble() + " ");
}
} /* Output:
0.7271157860730044 0.5309454508634242 0.16020656493302599 0.18847866977771732 0.5166020801268457 0.2678662084200585 0.2613610344283964
*///:~
二
//: interfaces/AdaptedRandomDoubles.java //读取字符数组 // Creating an adapter with inheritance. package object; import java.nio.*; import java.util.*; class CharSequence { 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(); char[] generate() { char[] buffer = new char[10]; int idx = 0; buffer[idx++] = capitals[rand.nextInt(capitals.length)]; for(int i = 0; i < 4; i++) { buffer[idx++] = vowels[rand.nextInt(vowels.length)]; buffer[idx++] = lowers[rand.nextInt(lowers.length)]; } buffer[idx] = ' '; return buffer; } } class E16_AdaptedCharSequence extends CharSequence implements Readable { private int count; public E16_AdaptedCharSequence(int count) { this.count = count; } public int read(CharBuffer cb) { if(count-- == 0) return -1; // Indicates end of input char[] buffer = generate(); cb.put(buffer); //将buffer字符数组输出到流 return buffer.length; } public static void main(String[] args) { Scanner s = new Scanner(new E16_AdaptedCharSequence(10)); while(s.hasNext()) System.out.println(s.next()); } }