In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you the Java7 diamond syntax and generic constructor example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to understand it!
The details are as follows:
A practical battle-- generic constructor
1 code
The T parameter in class Foo {public Foo (T t) {System.out.println (t);}} public class GenericConstructor {public static void main (String [] args) {/ / generic constructor is String. New Foo ("Crazy Java handout"); / / the T parameter in the generic constructor is Integer. New Foo / / explicitly specifies that the T parameter in the generic constructor is String, and / / the argument passed to the Foo constructor is also a String object, exactly. New Foo ("Crazy Android handout"); / / explicitly specify that the T parameter in the generic constructor is String, / / but the argument passed to the Foo constructor is the Double object, the following code error / / new Foo (12.3);}}
2 run
Crazy Java handout
two hundred
Crazy Android handout
The second actual combat-- the mixed use of generic constructor and diamond grammar
1 code
The E parameter in the class MyClass {public MyClass (T t) {System.out.println ("t parameter is:" + t);}} public class GenericDiamondTest {public static void main (String [] args) {/ / MyClass class declaration is of type String. / / the T parameter declared in the generic constructor is of type Integer MyClass mc1 = new MyClass (5); / / explicitly specifies that the T parameter declared in the generic constructor is of type Integer, MyClass mc2 = new MyClass (5); and / / the E parameter in the MyClass class declaration is of type String. / / if you explicitly specify that the T parameter declared in the generic constructor is of type Integer / / the "diamond" syntax cannot be used at this time, the following code is wrong. / / MyClass mc3 = new MyClass (5);}}
2 run
The value of the t parameter is: 5
The value of the t parameter is: 5
3 description
You cannot use diamond syntax if the program displays the actual type of formal parameter declared in the generic constructor.
This is all the content of the article "sample Analysis of Diamond Syntax and generic Constructor in Java7". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.