"Thinkinginjava" 44, Data generator

Source: Internet
Author: User

<pre name= "code" class= "java" >//: net/mindview/util/generator.java//A Generic Interface.package Net.mindview.util;public interface Generator<t> {T next ();}///:~


/*** Book: Thinking in Java * Function: Data Generator--Strategy design mode * File: countinggenerator.java* time: April 29, 2015 19:05:09* Author: cutter_point*/ Package Net.mindview.util;public class countinggenerator{//A Boolean static class public static classes Boolean implements Generator <java.lang.boolean>{private Boolean value = false; @Overridepublic Java.lang.Boolean Next () {// Returns a value that is the opposite of values value =!value;return value;}} Static class of byte type public static classes byte implements Generator<java.lang.byte>{private byte value = 0; @Overridepublic Java.lang.Byte Next () {///Next is add a bit and return the previous value of return value++;}} The static character array contains all the letters static char[] chars = ("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"). ToCharArray ();/ Character-type public static class Character implements Generator<java.lang.character>{int index = -1;//indexes @override// Get a string of characters inside the public Java.lang.Character next () {index = (index + 1)%chars.length;//look at the next character in all the letters of the first return Chars[index];}} String type public static class string implements Generator<java.lang.string>{private int length = 7; Generator<java.lang.Character> CG = new Character ();p ublic string () {}public string (int length) {this.length = length;} Public java.lang.String Next () {char[] buf = new Char[length];for (int i = 0; i < length; ++i) {Buf[i] = Cg.next ();} return new java.lang.String (BUF);}} Data public for short type static class short implements Generator<java.lang.short>{private short value = 0;@ Overridepublic Java.lang.Short Next () {return value++;}} public static class Integer implements Generator<java.lang.integer>{private int value = 0; @Overridepublic Java.lang.Integer Next () {return value++;}}    public static class Float implements Generator<java.lang.float> {private Float value = 0;      Public Java.lang.Float Next () {Float result = value;      Value + = 1.0;    return result; }}public Static class Double implements generator<java.lang.double>{private Double value = 0.0;public Java.lang.Double Next () {Double result = Value;value + = 1.0;return result;}} public static void MaiN (java.lang.string[] args)//{//byte B = new Byte (),//int i = B.next ();//system.out.println (i);////system.out.println ( New Character (). Next ());////string s = new String (9);////system.out.println (S.next ());/}}





/*** book: "Thinking in Java" * Function: Data Generator--strategy design mode--Test class * File: countinggenerator.java* time: April 29, 2015 19:05:09* Author: cutter_point* /package Lesson16arrays;import Net.mindview.util.countinggenerator;import Net.mindview.util.generator;public class generatorstest{public static int size = 10;public static void Test (Class<?> surroundingclass) {for (class<?> Type:surroundingClass.getClasses ())//Get the class inside this class object {//Get an array of this type System.out.println (Type.getsimplename () + ":"); try{ generator<?> g = (generator<?>) type.newinstance (); Gets an instantiated object//output his count for (int i = 0; i < size; ++i) {System.out.printf (G.next () + "");} System.out.println ();} catch (Exception e) {e.printstacktrace ();}}} public static void Main (string[] args) {test (countinggenerator.class);}}



Output:

Boolean:
True false to False to true false true false if
Byte:
0 1 2 3 4 5 6 7 8 9
Character:
A b c d e F g h i J
Double:
0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0
Float:
0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0
Integer:
0 1 2 3 4 5 6 7 8 9
Short:
0 1 2 3 4 5 6 7 8 9
String:
ABCDEFG hijklmn opqrstu vwxyzab cdefghi jklmnop qrstuvw xyzabcd Efghijk LMNOPQR





"Thinkinginjava" 44, Data generator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.