In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this "Java basic generic case Analysis" article, so the editor summarizes the following, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Java basic generic case Analysis" article.
1. Generics
Overview:
Generics, a feature introduced in JDK5, provides a compile-time type safety detection mechanism that allows illegal types to be detected at compile time.
It is essentially a parameterized type, that is, the data type being operated on is specified as a parameter
Parameterized type: to parameterize the type from the original specific type, and then pass in the specific parameters when using / calling
This parameter type can be used in classes, methods and interfaces, which are called generic classes, generic methods, and generic interfaces, respectively.
Define the format:
1: specify a type format, where the type can be regarded as a formal parameter
2: specify the format of multiple types, separated by multiple types, where the type can be regarded as a formal parameter
3. The given type can be regarded as an argument when a specific call is made in the future, and the type of the argument can only be a reference type.
Advantages of generics:
1. Advance run-time problems to compile time, making it easier to find and deal with
two。 Avoid forced type conversions
2. Generic classes
Define the format:
Format: modifier class class name {}
Example: public class Gneneric {}
The T here can be written as any logo, and common parameters such as T, E, K, V are often used to represent generics.
When a class uses generics, the member variables in the class are not limited to one type, as long as you declare what type it is when using it.
Generic class
Public class Generic {private T t; public T getT () {return t;} public void setT (T t) {this.t = t;}}
Call format:
Genericg=new Generic (); g.setT (102); III. Generic method
Format: modifier void method name (type parameter) {}
Example: public void show (T t) {}
Generic method:
Public class Generic {public void show (T t) {System.out.println (t);}}
Call format:
Public class GenericDemo {public static void main (String [] args) {Generic g = new Generic (); g.show (Zhang San); g.show (18); g.show (true);}} IV. Generic interface
Format: modifier interface interface name {}
Example: public interface Gneneric {}
Implementation format:
Generic interface
Public interface Generic {void show (T t);}
Generic interface implementation class
Public class GenericImpl implements Generic {@ Override public void show (T t) {System.out.println (t);}}
Test class
Public class GenericDemo {public static void main (String [] args) {GenericImpl g1=new GenericImpl (); g1.show ("Zhang San"); GenericImplg2=new GenericImpl (); g2.show (12); GenericImplg3=new GenericImpl (); g3.show (true); Genericg4=new GenericImpl (); g4.show (13.15);} V, type wildcards
To represent the parents of various generic List, you can use type wildcards
1.List: a List that represents the location of an element type, whose elements can match any type
two。 This wildcard List only means that it is the parent of various generic List and cannot add elements to it.
If we don't want List to be the parent of any generic List, we just want it to represent the parent of a certain type of generic List, and we can use the upper limit of type wildcards.
1. Upper limit of type wildcards:
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.