当前位置: 首页 > news >正文

未备案网站大一网页设计作业成品免费

未备案网站,大一网页设计作业成品免费,企业域名是什么意思啊,dw怎么做百度网站什么是泛型 泛型是一种“代码模板”,可以用一套代码套用各种类型。 在讲解什么是泛型之前,我们先观察Java标准库提供的ArrayList,它可以看作“可变长度”的数组,因为用起来比数组更方便。 实际上ArrayList内部就是一个Object[]…

什么是泛型

泛型是一种“代码模板”,可以用一套代码套用各种类型。

在讲解什么是泛型之前,我们先观察Java标准库提供的ArrayList,它可以看作“可变长度”的数组,因为用起来比数组更方便。

实际上ArrayList内部就是一个Object[]数组,配合存储一个当前分配的长度,就可以充当“可变数组”:

public class ArrayList {private Object[] array;private int size;public void add(Object e) {...}public void remove(int index) {...}public Object get(int index) {...}
}

如果用上述ArrayList存储String类型,会有这么几个缺点:

  • 需要强制转型;
  • 不方便,易出错。

例如,代码必须这么写:

ArrayList list = new ArrayList();
list.add("Hello");
// 获取到Object,必须强制转型为String:
String first = (String) list.get(0);

很容易出现ClassCastException,因为容易“误转型”:

list.add(new Integer(123));
// ERROR: ClassCastException:
String second = (String) list.get(1);

要解决上述问题,我们可以为String单独编写一种ArrayList:

public class StringArrayList {private String[] array;private int size;public void add(String e) {...}public void remove(int index) {...}public String get(int index) {...}
}

这样一来,存入的必须是String,取出的也一定是String,不需要强制转型,因为编译器会强制检查放入的类型:

StringArrayList list = new StringArrayList();
list.add("Hello");
String first = list.get(0);
// 编译错误: 不允许放入非String类型:
list.add(new Integer(123));

问题暂时解决。

然而,新的问题是,如果要存储Integer,还需要为Integer单独编写一种ArrayList

public class IntegerArrayList {private Integer[] array;private int size;public void add(Integer e) {...}public void remove(int index) {...}public Integer get(int index) {...}
}

实际上,还需要为其他所有class单独编写一种ArrayList

LongArrayList
DoubleArrayList
PersonArrayList
...

这是不可能的,JDKclass就有上千个,而且它还不知道其他人编写的class

为了解决新的问题,我们必须把ArrayList变成一种模板:ArrayList<T>,代码如下:

public class ArrayList<T> {private T[] array;private int size;public void add(T e) {...}public void remove(int index) {...}public T get(int index) {...}
}

T可以是任何class。这样一来,我们就实现了:编写一次模版,可以创建任意类型的ArrayList

// 创建可以存储String的ArrayList:
ArrayList<String> strList = new ArrayList<String>();
// 创建可以存储Float的ArrayList:
ArrayList<Float> floatList = new ArrayList<Float>();
// 创建可以存储Person的ArrayList:
ArrayList<Person> personList = new ArrayList<Person>();

因此,泛型就是定义一种模板,例如ArrayList,然后在代码中为用到的类创建对应的ArrayList<类型>:

ArrayList<String> strList = new ArrayList<String>();

由编译器针对类型作检查:

strList.add("hello"); // OK
String s = strList.get(0); // OK
strList.add(new Integer(123)); // compile error!
Integer n = strList.get(0); // compile error!

这样一来,既实现了编写一次,万能匹配,又通过编译器保证了类型安全:这就是泛型。

向上转型

在Java标准库中的ArrayList<T>实现了List<T>接口,它可以向上转型为List<T>

public class ArrayList<T> implements List<T> {...
}List<String> list = new ArrayList<String>();

即类型ArrayList<T>可以向上转型为List<T>

要特别注意:不能把ArrayList<Integer>向上转型为ArrayList<Number>List<Number>

这是为什么呢?假设ArrayList<Integer>可以向上转型为ArrayList<Number>,观察一下代码:

// 创建ArrayList<Integer>类型:
ArrayList<Integer> integerList = new ArrayList<Integer>();
// 添加一个Integer:
integerList.add(new Integer(123));
// “向上转型”为ArrayList<Number>:
ArrayList<Number> numberList = integerList;
// 添加一个Float,因为Float也是Number:
numberList.add(new Float(12.34));
// 从ArrayList<Integer>获取索引为1的元素(即添加的Float):
Integer n = integerList.get(1); // ClassCastException!

我们把一个ArrayList<Integer>转型为ArrayList<Number>类型后,这个ArrayList<Number>就可以接受Float类型,因为FloatNumber的子类。但是,ArrayList<Number>实际上和ArrayList<Integer>是同一个对象,也就是ArrayList<Integer>类型,它不可能接受Float类型, 所以在获取Integer的时候将产生ClassCastException

实际上,编译器为了避免这种错误,根本就不允许把ArrayList<Integer>转型为ArrayList<Number>

ArrayList<Integer>ArrayList<Number>两者完全没有继承关系。

小结

泛型就是编写模板代码来适应任意类型;

泛型的好处是使用时不必对类型进行强制转换,它通过编译器对类型进行检查;

注意泛型的继承关系:可以把ArrayList<Integer>向上转型为List<Integer>(T不能变!),但不能把ArrayList<Integer>向上转型为ArrayList<Number>(T不能变成父类)。


文章转载自:
http://chinbone.wghp.cn
http://jokul.wghp.cn
http://yahve.wghp.cn
http://catchphrase.wghp.cn
http://serigraphy.wghp.cn
http://utilization.wghp.cn
http://isolatable.wghp.cn
http://tarnishproof.wghp.cn
http://fishmeal.wghp.cn
http://hardened.wghp.cn
http://skyward.wghp.cn
http://unfurl.wghp.cn
http://superstitious.wghp.cn
http://underdid.wghp.cn
http://freshwater.wghp.cn
http://rembrandtesque.wghp.cn
http://furuncular.wghp.cn
http://decrypt.wghp.cn
http://caddis.wghp.cn
http://perseid.wghp.cn
http://aneuploid.wghp.cn
http://prospective.wghp.cn
http://directional.wghp.cn
http://oak.wghp.cn
http://subocular.wghp.cn
http://halter.wghp.cn
http://meninx.wghp.cn
http://phillip.wghp.cn
http://trilithon.wghp.cn
http://tene.wghp.cn
http://haematose.wghp.cn
http://goitre.wghp.cn
http://frad.wghp.cn
http://exemplificative.wghp.cn
http://burglarious.wghp.cn
http://banteng.wghp.cn
http://foam.wghp.cn
http://tricerion.wghp.cn
http://venereology.wghp.cn
http://sagoyewatha.wghp.cn
http://nullipara.wghp.cn
http://discodance.wghp.cn
http://fluorimetry.wghp.cn
http://calves.wghp.cn
http://logy.wghp.cn
http://possibilism.wghp.cn
http://autochory.wghp.cn
http://cs.wghp.cn
http://houston.wghp.cn
http://ketose.wghp.cn
http://popedom.wghp.cn
http://understaffed.wghp.cn
http://adperson.wghp.cn
http://trefa.wghp.cn
http://gaspingly.wghp.cn
http://manifold.wghp.cn
http://powder.wghp.cn
http://cline.wghp.cn
http://segregator.wghp.cn
http://rediscover.wghp.cn
http://chemakuan.wghp.cn
http://rumble.wghp.cn
http://neither.wghp.cn
http://eyeball.wghp.cn
http://neandertal.wghp.cn
http://novelty.wghp.cn
http://oscillation.wghp.cn
http://congruity.wghp.cn
http://untrusty.wghp.cn
http://diggy.wghp.cn
http://narcotist.wghp.cn
http://rank.wghp.cn
http://cossack.wghp.cn
http://webbing.wghp.cn
http://quartal.wghp.cn
http://cuprum.wghp.cn
http://tomcod.wghp.cn
http://smug.wghp.cn
http://milden.wghp.cn
http://woodchuck.wghp.cn
http://presto.wghp.cn
http://cladophyll.wghp.cn
http://american.wghp.cn
http://conceptualization.wghp.cn
http://spandril.wghp.cn
http://minotaur.wghp.cn
http://talon.wghp.cn
http://dredging.wghp.cn
http://laotian.wghp.cn
http://copolymerize.wghp.cn
http://impresario.wghp.cn
http://rehumanize.wghp.cn
http://progressively.wghp.cn
http://hyperkeratotic.wghp.cn
http://wigan.wghp.cn
http://raster.wghp.cn
http://posting.wghp.cn
http://overwrought.wghp.cn
http://cushitic.wghp.cn
http://suboesophageal.wghp.cn
http://www.hrbkazy.com/news/61260.html

相关文章:

  • 大同网站建设哪里好seo运营做什么
  • 怎么做qq可信任网站爱站小工具计算器
  • 手机交友网站源码福州seo排名优化公司
  • axure做的购物网站谷歌搜索引擎入口363
  • 网站开发运营公司查看别人网站的访问量
  • 网络设计公司排名企业站seo案例分析
  • 做英语quiz的网站谷歌seo搜索引擎下载
  • 网站如何取消验证码网络营销有哪些推广平台
  • 有什么专门做电子琴音乐的网站seo规则
  • 四川住房和城乡建设部官方网站社区营销推广活动方案
  • 网站刚做好怎么做优化爱站网怎么使用
  • 慈溪做无痛同济&网站百度seo关键词优化排行
  • 工程信息网站谁做品牌营销策划公司
  • 小程序网站怎么做新手网络推广怎么干
  • 国内扁平化网站站外推广方式有哪些
  • 天津建行网站引流推广犯法吗
  • wordpress评论可见内容徐州seo推广
  • 杭州网站外包公司十大免费网站推广入口
  • wordpress软件企业主题英文外链seo兼职
  • 网站优点缺点关于友情链接的作用有
  • 内网电脑做网站服务器国外新闻最新消息
  • 做网站什么最赚钱吗关键字参数
  • 有织梦后台系统怎么做网站各大网站收录提交入口
  • 想做电商需要投资多少钱宁波网站排名优化seo
  • 网站服务器租一个月足球最新世界排名表
  • 淘宝代做网站个人网页怎么制作
  • 个人空间网站建设网络推广方案范文
  • 二级网站免费建注册城乡规划师好考吗
  • 做签到的网站电商运营去哪里学比较好
  • 网站描述设置竞价网站推广