问一个java语言规范纯理论问题

无名老僧 2012-06-12 03:18:20
A variable of an interface type can hold a null reference or a reference to any
instance of any class that implements the interface.
Note that a variable is not guaranteed to always refer to a subtype of its declared type, but
only to subclasses or subinterfaces of the declared type. This is due to the possibility of
heap pollution discussed below.

------The Java™ Language Specification Java SE 7 Edition 第4章 4.12.2

请问上面讲到的subtype 跟subclass,subinterface分别指的是什么?
...全文
127 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
回收站666 2012-06-12
  • 打赏
  • 举报
回复
补充一下,好型泛型不属于这个行列,出现泛型的地方,我认为可以都把它替换成Object,Java的泛型采用的是类型擦除,比如:类定义class A<T>{},定义变量 A<Integer> a;应该等同于Object a;泛型的继承也不是简单的类继承,比如:A<Number> a1; A<Integer> a2=new A<>();(new A<>()属于Java7的菱形推断,Java7之前用new A<Integer>()),a1=a2;(这条语句是错误的,可以理解为A<Integer>并不继承自A<Number>,虽然Integer继承Number,具体的翻译一下,Object a1;Object a2=new Object();说明a2具有的类型并不是继承自a1所具有的类型。这里看似好想a1=a2可以是对的,但是Java泛型的设计者为了安全考虑,也屏蔽了底层的Java虚拟机,所以a1=a2在Java编译器编译时是通不过的。)。最后,Java的泛型的继承,以及使用,如果完全理解其原理,一句两句也说不清楚,涉及Java虚拟机,Java编译器,Java语言语法等之间的关系理解。好像听说Java的后续版本会在虚拟机层面支持泛型,具体的理解,正确与否,参见Oracle的官方文档资料。
回收站666 2012-06-12
  • 打赏
  • 举报
回复
subclass子类应该就是类继承中的下层类,subinterface应该是接口继承关系中的下层接口。subclass还是类,subinterface还是接口,接口继承使用的还是extends关键字。subtype子类型,我认为应该是广泛的概念吧,可以认为是子类或子接口。《A variable of an interface type can hold a null reference or a reference to any instance of any class that implements the interface.》总之一句话,一个接口类型的变量可以被null空引用或被实现了这个接口的任何类型的类任何实例赋值。这里的类包括直接实现类,间接实现类,间接实现类又多了,包括继承了子类的类和子接口的实现类。楼主慢慢体会,英文看多了自然就明白了!《The Java™ Language Specification Java SE 7 Edition》这个我也看完了,但是当时还真没考虑这个。《The Java™ Virtual Machine Specification Java SE 7 Edition》这个也可以看看!
xlhb 2012-06-12
  • 打赏
  • 举报
回复
全是英语啊
无名老僧 2012-06-12
  • 打赏
  • 举报
回复
是否可以这样理解:
假设有类定义 :class A<T>{}

那么下面的调用:
A<Integer> a=new A<>();

其中变量 a 的type是 A<Integer>,a的class 是 A,这样理解对吗?
无名老僧 2012-06-12
  • 打赏
  • 举报
回复
开桌面词典慢慢看,问题不大。
a363722188 2012-06-12
  • 打赏
  • 举报
回复
全是英语啊 楼主是外星人呢
无名老僧 2012-06-12
  • 打赏
  • 举报
回复
郁闷了,不知道他这里type跟class,interface的区别是什么?难道subtype不就是subclass跟subinterface吗?

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧