JAVA基础教程9

上传人:飞*** 文档编号:54580754 上传时间:2018-09-15 格式:PPT 页数:9 大小:381KB
返回 下载 相关 举报
JAVA基础教程9_第1页
第1页 / 共9页
JAVA基础教程9_第2页
第2页 / 共9页
JAVA基础教程9_第3页
第3页 / 共9页
JAVA基础教程9_第4页
第4页 / 共9页
JAVA基础教程9_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《JAVA基础教程9》由会员分享,可在线阅读,更多相关《JAVA基础教程9(9页珍藏版)》请在金锄头文库上搜索。

1、Type information,华南理工大学 计算机科学与工程学院 韦佳 ,The need for RTTI,/Shapes.java The upcast occurs when the shape is placed into the List. The RTTI occurs when you fetch an element out of the array. Polymorphism takes then.,The Class object,RTTI is accomplished through a special kind of object called the Class

2、 object, which contains information about the class. Each time you write and compile a new class, a single Class object is also created (stored in an identically named .class file). All classes are loaded into the JVM dynamically, upon the first use of a class.,Once the Class object for that type is

3、 in memory, it is used to create all objects of that type. /SweetShop.java One of the ways to get a reference to the Class object is the static forName() method. The call to forName() is being made for its side effect in the last example, which is to load the class Gum if it isnt already loaded.,If

4、you already have an object of the type youre interested in, you can fetch the Class reference by calling a method thats part of the Object root class: getClass(). /ToyTest.java,Class literals,Java provides a second way to produce the reference to the Class object: the class literal. FancyToy.class;

5、Creating a reference to a Class object using “.class” doesnt automatically initialize the Class object. /ClassInitialization.java,Generic class references,Using the generic syntax, you can constrain the type of Class object. /GenericClassReferences.java To loosen the constraints when using generic C

6、lass references, we can employ wildcard?. /WildcardClassReferences.java In order to create a Class reference that is constrainted to a type or any subtype, we can combine the wildcard with the extends keyword. /BoundedClassReferences.java,Checking before a cast,Theres a third form of RTTI in Java. This is the keyword instanceof, which tells you if an object is an instance of a particular type. Like this: if(x instanceof Dog) (Dog)x).bark(); /PetCreator.java; ForNameCreator.java; PetCount.java,To be continued,

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号