In this section, let's show you how to build complex models with generics.
1. Tuple list
We've said that before. Tuples are a complex model that can return multiple objects.
Package Com.ray.ch11;import Java.util.arraylist;public class Test {public Arraylist<tuple<a, B, c>> Test () { Arraylist<tuple<a, b, c>> list = new Arraylist<tuple<a, B, c>> (); for (int i = 0; i <; i++) { List.add (New Tuple<a, B, c> (New A (), New B (), New C ())); return list;} public static void Main (string[] args) {new test (). Test ();}} Class A {}class B {}class C {} @SuppressWarnings ("hiding") class Tuple<a, B, c> {public final A a;public final B b;pub LIC final C c;public Tuple (a A, b b, c c) {THIS.A = a;this.b = B;THIS.C = C;}}
The above code allows us to implement a more complex model through tuples.
We will refer to another example below. A store.
2. Shop
This shop consists of office area, front desk, sales area, and the sales area consists of a number of shelves, shelves above the need to place a variety of goods.
Package Com.ray.ch11;import Java.util.arraylist;import Java.util.collection;import Java.util.random;public class Store extends arraylist<salezone> {Private office office = new Office ();p rivate CheckOut CheckOut = new CheckOut ();p Ublic Store (int salezonenum, int shelfnum, int producenum) {for (int i = 0; i < Salezonenum; i++) {Add (new Salezone (She Lfnum, Producenum));}} public static void Main (string[] args) {new Store (1, 2, 5);}} Class Product {private int id = 0;private String name = "";p rivate double price = 0.0;public Product (int id, string name, Double price) {this.id = Id;this.name = Name;this.price = Price; System.out.println (ToString ());} public static generator<product> Generator = new generator<product> () {@Overridepublic Product next () { Random random = new random (), int id = random.nextint (), return new Product (ID, "test-" + ID, random.nextdouble ());}}; @Overridepublic String toString () {return ' Produce ID: ' + ID + ' name: ' + name + ' Price: ' + Price;}} InterfaCe generator<t> {public T next ();} Class Generators {public static <T> collection<t> Fill (collection<t> collection,generator<t> generator, int num) {for (int i = 0; i < num; i++) {Collection.add (Generator.next ());} return collection;}} Class Shelf extends Arraylist<product> {/** * */private static final long serialversionuid = 1l;public Shelf (int pr Oducenum) {Generators.fill (this, product.generator, Producenum);}} Class Salezone extends Arraylist<shelf> {/** * */private static final long serialversionuid = 1l;public Salezone (in T shelfnum, int producenum) {for (int i = 0; i < Shelfnum; i++) {Add (new Shelf (Producenum));}}} Class Office {}class CheckOut {}
You might understand that the code above is a little more complicated, and I'll explain:
1. The first difficulty lies in the generator, assuming that reading the previous chapters might be a little simpler.
In fact, the generator is used here primarily to abstract a relatively generic generator. Assuming that the code is generic, we can directly return a producelist in product, which may seem like a lot better.
2.Generators, the main is to abstract the container to fill the data of the universal code.
3. There are several classes that directly inherit the ArrayList, this is to be able to call the Add method directly at the time of the constructor, without having to construct the ArrayList once. Assuming the usual habit, maybe we'll build a ArrayList and fill it with data.
4. Use the anonymous inner class to create a generator inside the product.
Summary: This chapter focuses on how to build complex models using generics.
This chapter is here, thank you.
-----------------------------------
Folder
Meet java-13.5 from the beginning to build complex models with generics