Java如何处理对泛型类型的引用 [英] How java deals with references to a generic type

查看:169
本文介绍了Java如何处理对泛型类型的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java如何通过引用存储和处理通用信息.

How java stores and deals with generic information with references.

List<A>List<?>的子类型.请解释以上声明.继承如何与对通用对象的引用一起工作.

List<A> is a subtype of List<?>. Please explain above statement. How inheritance works with references to a generic object.

推荐答案

来自 Java文档

使用通配符(?)(例如,列表)指定无限制的通配符类型.这称为未知类型列表.在两种情况下,无界通配符是一种有用的方法:

The unbounded wildcard type is specified using the wildcard character (?), for example, List. This is called a list of unknown type. There are two scenarios where an unbounded wildcard is a useful approach:

  1. 如果您要编写一种可以使用Object类提供的功能实现的方法.
  2. 当代码使用泛型类中不依赖于type参数的方法时.

unbounded (?)移到 bounded type()正在从 Generic 变为 Specific 实体

Moving from unbounded(?) to bounded type() is moving away from Generic to Specific entity.

让我看一下我自己的例子.

let's go through my own example.

  1. 地球,一个行星具有生物

动物树木是一些活物

人类是具有2条腿和2只手的动物

Humans are animal with 2 legs and 2 hands

男人和女人女人是人类的两种类型.

如果有?在列表中,您可以将列表应用于生活.但是,如果您希望列表仅包含人类或男人女性,则可以根据需要决定边界类型.

If you have ? in List, you can apply List to Living things. But if you want your list to contain only Humans or Men or Women, you will decide bounded type as per your need.

从通用到特定:列表< ? >,列表<生物>,列表<人类>,列表<男士>,您正在将更具体的内容应用于您的实体.

As you move from generic to specific : List < ? > , List < Living things >, List < Humans >, List< Men >, you are applying more specific things to your entity.

树是像人类或人类一样的生物,但它不能像人类一样有2条腿和2只手,而且它也无法思考.

您必须确定要将实体绑定到特定类型的特定级别.

You have to decide at which specific level, you want to bind the entity to specific type.

这篇关于Java如何处理对泛型类型的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆