Learn from the beginning java-13.9 implicit and displayed creation type instances

Source: Internet
Author: User
Tags object object

It is too simple for the reader to propose the problems caused by the erasure of the previous chapter and how to solve them. Here to explain: Because the author itself can not meet the corresponding problem. Just imagine some of the possible problems, based on a limited level, so the previous chapter is relatively simple to write, welcome the broad masses of readers to comment, I will try to improve.

Return to this section, where we will discuss the implicit and displayed creation type instances.

1. Implicitly-constructed type instances

We mentioned the following problematic code in the previous section:

Package Com.ray.ch11;public class Test<t> {public void Test (Object object) {//system.out.println (object instanceof t);//error//system.out.println (New T ());//error//system.out.println (new t[10]);// ErrorSystem.out.println ((T) object);//waring}}


The solution is the following code:

Package Com.ray.ch11;import Java.lang.reflect.array;public class Test<t> {private class<t> Kind;public Test (Class<t> t) {kind = t;} public void Test (Object object) {System.out.println (Kind.isinstance (object)); try {System.out.println ( Kind.newinstance ());} catch (Instantiationexception e) {e.printstacktrace ();} catch (Illegalaccessexception e) {e.printstacktrace ();} System.out.println (array.newinstance (kind, 10)); System.out.println ((T) object);//Waring}}


However, there is not much problem on the surface, since we have a default constructor based on this class, let's take a look at the following code:

Package Com.ray.ch11;public class Test<t> {private class<t> kind;public Test (class<t> t) {kind = T;} public void Test () {try {System.out.println (Kind.newinstance ())} catch (Instantiationexception e) {e.printstacktrace ( );} catch (Illegalaccessexception e) {e.printstacktrace ();}} public static void Main (string[] args) {new test<integer> (Integer.class). Test ();}}

Output:

Java.lang.InstantiationException:java.lang.Integer
At Java.lang.Class.newInstance0 (class.java:340)
At Java.lang.Class.newInstance (class.java:308)
At Com.ray.ch11.Test.test (test.java:12)
At Com.ray.ch11.Test.main (test.java:21)

We lowered some of the code, but the subject remained. Then we put the integer in the class, we can see from the output, direct error. The reason is that the integer does not have a default constructor.

Because of the problems that appear above, we recommend the following to show how the type object is created.

2. Instances of created types displayed

(1) Using factory objects to constrain implementation

Package Com.ray.ch11;public class Test<t> {public static void main (string[] args) {new Integerfactory (). Create ();}} Interface Ifactory<t> {T create ();} Class Integerfactory implements ifactory<integer> {@Overridepublic integer create () {return new Integer (0);}}


(2) through the template method design mode to constrain the implementation

Package Com.ray.ch11;public class Test<t> {public static void main (string[] args) {new Integerfactory (). Show ();}} Abstract class Afactory<t> {public final T obj;public afactory () {obj = Create ();} Abstract public T Create ();} Class Integerfactory extends afactory<integer> {@Overridepublic integer create () {return new integer (0);} public void Show () {System.out.println (Obj.getclass (). GetName ());}}


Summary: This chapter focuses on the implicit creation of implicitly created type instances, and how to create a type instance by showing how to resolve it.

This chapter is here, thank you.

-----------------------------------

Folder

Learn from the beginning java-13.9 implicit and displayed creation type instances

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.