In this chapter we will discuss generics and containers, which are precisely the generic requirements that arise from the requirements of containers.
Most of our programming time uses containers to load objects. Let's see how containers evolve.
1. Ordinary containers
package com.ray.ch11; public class Test { public static void main(String[] args) { Holder holder = new Holder(new RealObject()); RealObject realObject = holder.getRealObject(); } } class RealObject { static { System.out.println("loading object"); } } class Holder { private RealObject realObject; public Holder(RealObject realObject) { this.realObject = realObject; } public RealObject getRealObject() { return realObject; } }
The code above shows a container that can hold objects of this type, RealObject, but is less practical because it can only store a single type.
Universal Container
package com.ray.ch11; public class Test { public static void main(String[] args) { Holder holder = new Holder(new RealObject()); RealObject realObject = (RealObject) holder.getObject(); Holder holder2 = new Holder(new RealObject2()); RealObject2 realObject2 = (RealObject2) holder2.getObject(); } } class RealObject { static { System.out.println("loading object"); } } class RealObject2 { static { System.out.println("loading object2"); } } class Holder { private Object object; public Holder(Object object) { this.object = object; } public Object getObject() { return object; } }
Let's modify the code above to change the type that holder loads to Object, where Holder can load a container of any type of object.
Although the container above is omnipotent, you will not use any type of object in general programming, most of the time you store a single type of object, and the need to cast during use is a big problem, because you do not necessarily know the type of object you need to convert, so you need to quoteEnter generic.
3. Generic Containers
package com.ray.ch11; public class Test { public static void main(String[] args) { Holder<RealObject> holder = new Holder<RealObject>(new RealObject()); RealObject realObject = (RealObject) holder.getObject(); Holder<RealObject2> holder2 = new Holder<RealObject2>(new RealObject2()); RealObject2 realObject2 = (RealObject2) holder2.getObject(); } } class RealObject { static { System.out.println("loading object"); } } class RealObject2 { static { System.out.println("loading object2"); } } class Holder<T> { private T object; public Holder(T object) { this.object = object; } public Object getObject() { return object; } }
We will modify the container again to introduce generic features, at which point the container basically meets the requirements, and type checking can also be performed during compilation to ensure the correctness of the type.
4. Core concepts of generics
That's when I tell the compiler what type of container I want to use, and the compiler handles some details as needed (for example, type detection)
Summary: This chapter focuses on the evolution of containers and the relationship between generics and containers.
Thank you for this chapter.
-----------------------------------
Reprinted at: https://my.oschina.net/u/2325575/blog/543731