当前位置:网站首页>The first day of handwritten RPC -- review of some basic knowledge

The first day of handwritten RPC -- review of some basic knowledge

2022-06-24 21:19:00 datoooooou

Start preparing your resume today , That is to say, those who are going to ruin the street rpc


Learning content :

Thank you first Guide Brother provides us with ideas :

https://github.com/CN-GuoZiyang/My-RPC-Framework;

Then there is the more easy to understand version written by brother Sheng :
https://github.com/CN-GuoZiyang/My-RPC-Framework;

Finally, a comment version shared by brother Niuke ( The kind of food that comes to your mouth ):https://github.com/IAMPadA/Panda-Rpc-Framework/tree/master

It's a pity that even if I knock on other people's projects, there will still be a lot of things confused , Eh, this <> It's something , Oh, like before javaSE I heard from Master Kang that it is called generics , What's a question mark in it ???

OK This concludes today's project , Let's go and figure out the generics first , Otherwise, all the methods are well written


Learning output :

After passing Niuke 、CSDN、 And another baptism of Master Kang , It's probably to make the generics clear , Incidentally, I also read the common interview questions , Here I recommend a blog that speaks a lot :

https://blog.csdn.net/s10461/article/details/53941091?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164527353216780255238521%2522%252C%2522scm%2522%253A%252220140713.130102334…%2522%257D&request_id=164527353216780255238521&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2alltop_positive~default-1-53941091.first_rank_v2_pc_rank_v29&utm_term=%E6%B3%9B%E5%9E%8B&spm=1018.2226.3001.4187

My summary is as follows :
Generics are also called “ Parameterized types ”, It's about defining classes 、 Interface 、 Method to specify a specific type , Let class 、 Interface 、 Method to decide which type of parameter to use .

Java The generics of are in 1.5 Introduced , Only do generic checking at compile time , Runtime generics will disappear , We call this “ Generic erase ”, Eventually, the type will become Object. Before there are no generics , Every object read from the collection must be typed , If you accidentally insert the wrong type object , There will be errors in the transformation processing at run time , With generics , You can tell the compiler what type of object each collection receives , The compiler does type checking at compile time , Tells if an object of the wrong type has been inserted , Make the program more secure , And more clearly .

Generics are used in three ways , Respectively : Generic classes 、 Generic interface 、 Generic methods .

Generic class writing :
Class name <T> among T Represents a type name , I could just write , When instantiating, you must specify the specific type , A generic type parameter can only be a class type ( Including custom classes ), It can't be a simple type , That is, it cannot be int, It is Integer.

At the same time pay attention to : Generic types of classes cannot be used in static methods , It can be understood that the static structure is earlier than the creation of the class , Generics know the type only when they are instantiated , So it will report a mistake ; In addition, exception classes cannot be generic .

For multiple types, the declaration method is : Class name <T1,T2>

Generic interface : It is basically consistent with generic classes , The difference is that subclasses need to declare generic types at the interface when inheriting generic interfaces , Otherwise the compiler reports an error .

Generic methods : That is, generic structures appear in methods , It's different than before , Generic methods can be declared static , Because generic parameters are determined when a method is called , Instead of instantiating , The usual definition is :
Such as public <T> List<T> add(T[] arr)

Generics in terms of inheritance , if A yes B Parent class of ,G<A> and G<B> There is no parent-child relationship , It's a juxtaposition .

Finally, wildcards “?” Use , It can be understood as a common parent of two generics , Above AB The common parent of is G<?>, But you can't add any data to it (NULL With the exception of ), Similar to abstract classes .

The above is an unbounded wildcard , That is, there are no restrictions , There is also a restricted wildcard ,
It can be simply understood as :extends T Only generics are allowed to be T And its subclasses call
super T Only generics are allowed to be T Class and its parent class call
 Insert picture description here
It seems that 80% of them are so many , People are numb , When you are empty, look back and see if there is any mistake

原网站

版权声明
本文为[datoooooou]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211317188299.html