当前位置:网站首页>How to create a subtype like relationship between two generic classes when the classes are generic related
How to create a subtype like relationship between two generic classes when the classes are generic related
2022-06-29 04:17:00 【Ah Q said code】
Hello, everyone , I am Q!
It's like this ......
The screenshot of the conversation is as follows :
Yes Q The explanation of , Are you with “ Ma Xiaotiao ” There are also questions ? Please look
We all know that java in , As long as it is type compatible , You can assign objects of one type to objects of another type . For example, you can put a Integer Objects of type are assigned to Object Object of type , because Object yes Integer Superclass of .
Object someObject = new Object(); Integer someInteger = new Integer(10); someObject = someInteger; //OK
In object orientation , We call it is a The relationship between . because Integer yes Object A seed of , So it is allowed to be assigned .
Again because Integer It's also Number A seed of , So the code below is also valid :
public void someMethod(Number n) { /* ... */ }
someMethod(new Integer(10)); // OK
someMethod(new Double(10.1)); // OK
Of course, the same is true of generics , When executing a generic type call , take Number Pass as its type parameter , If the parameter is Number Subtypes of , Then any subsequent add call :
Box<Number> box = new Box<Number>(); box.add(new Integer(10)); // OK box.add(new Double(10.1)); // OK
Now let's look at the following code :
public void boxTest(Box<Number> n) { /* ... */ }
What type of parameters does the method receive ?
Through this method , You must know that its parameter type is Box<Number>, But let's think about a problem : Do you think Box<Integer> and Box<Double> Can parameters of type be passed in ?
The answer is no Of .
Even though Integer yes Number Subtypes of , but Box<Integer> and Box<Double> No Box<Number> Subclasses of , Their parent objects are Object. This is what the dialogue at the beginning of the text means .
So here comes the question , When the generics of a class are related , How to create a subtype like relationship between two generic classes ? For example, how to make Box<Integer> and Box<Double> Become with Box<Number> About ?
In order to understand this problem , Let's first understand how objects of the same type are subtyped .
By analyzing the source code, we can find that :ArrayList<E> Realized List<E>,List<E> Inherited Collection<E>, therefore ArrayList<String> yes List<String> Subtypes of , List<String> yes Collection<String> Subtypes of . So when we pass parameters ,ArrayList<String> The type can give List<E> perhaps Collection<E> Delivered .
As long as you don't change the type parameter , The subtype relationship between types will remain .
If we want to define our own list interface PayloadList, Make generic types P The optional value of is associated with each element , It can be defined as follows :
interface PayloadList<E,P> extends List<E> {
void setPayload(int index, P val);
...
}
be PayloadList<String,String>、PayloadList<String,Integer>、PayloadList<String,Exception> All are List<String> Subtypes of .
Summary : It can be subtyped by inheriting generic classes or implementing interfaces .
Understand the problem of subtype , We go back to “ How to create a subtype like relationship between two generic classes “ The problem of .
Generic classes or interfaces do not become relevant simply because of the relationship between their types , If you want to achieve the relevant , We can use wildcard To create relationships between generic classes or interfaces .
Box<Integer> and Box<Number> The parent object of is actually Box<?>
To create relationships between these classes , So that the code can pass Box<Integer> visit Box<Number> Methods , You can use the upper limit wildcard :
Box<? extends Integer> initBox = new Box<>(); Box<? extends Number> numberBox = initBox; // OK. Box<? extends Integer> is a subtype of Box<? extends Number>
because Integer yes Number Subtypes of ,numberBox The generics of are Number Object subclasses , So in intBox and numberBox There is a relationship between .
The picture is for use ceiling and Lower limit The relationship between several classes declared by wildcards .
边栏推荐
- If I hadn't talked to Ali P7, I wouldn't know I was a mallet
- Analysis of moudo Network Library
- 1016 部分A+B
- CDC2.2.1还不支持postgresql14.1么?基于pgbouncer连接方式下,以5433
- How sqlserver queries and removes results with null fields in the whole column
- Airflow 2.2.3 containerized installation
- 女程序员晒出5月的工资条:工资是高,但是真累,网友评论炸锅了
- NotImplementedError: Could not run torchvision::nms
- 1015 德才论
- Inftnews | metauniverse technology will bring a new shopping experience
猜你喜欢

How sqlserver queries and removes results with null fields in the whole column

moudo网络库剖析

赚钱的5个层次,你在哪一层?

Libuv库概述及libevent、libev、libuv对比(转载)

Blue Bridge Cup ruler method

科技雲報道:混合辦公的B面:安全與效率如何兼得?

开发者方案 · 环境监测设备(小熊派物联网开发板)接入涂鸦IoT开发平台

Redis 缓存穿透、缓存击穿、缓存雪崩

女程序员晒出5月的工资条:工资是高,但是真累,网友评论炸锅了

If you choose the right school, you can enter Huawei as a junior college. I wish I had known
随机推荐
Emotional changes need to be controlled
源代码防泄露技术种类浅析
干货丨微服务架构是什么?有哪些优点和不足?
pytorch 读写文件
Talking about Canary deployment
Call snapstateon closed sou from Oracle CDC
Is the sink usually the JDBC insert update delete?
yolox出现 RuntimeError: DataLoader worker (pid(s) 17724, 1364, 18928) exited unexpectedly
The great gods take connections from the MySQL connection pool in the open of the rich function. The initialization of the connection pool is 20. If the parallelism of the rich function is 1
【C语言】解决 “address of stack memory associated with local variable ‘num‘ returned”
请问大佬,Oracle CDC报错 Call snapshotState on closed sou
Ansible最佳实践之Playbook不同上下文提权Demo
Hot renewal process
String differences between different creation methods
Blue Bridge Cup ruler method
Apifox : 不仅是Api调试工具,更是开发团队的协作神器
为什么说测试岗位是巨坑?8年测试人告诉你千万别上当
LabVIEW显示Unicode字符
Nuxt - set SEO related tags, page titles, icons, etc. separately for each page (page configuration head)
NotImplementedError: Could not run torchvision::nms