是这样的,我只得只是编译能过,并不是实际运行的时候。当一个旧类转换成新类的时候,必须其中一个对象是另一个对象的子类。 下面以Son类继承Father类为例,其中必须有继承关系: public class Son extends Father{ public static void main(String [] args) {
Son son = new Son(); Father father ; father = (Father)son;
}
}class Father {} 但是为什么interface没有此限制?下面把Father类改为接口,并且Son不去implements它public class Son { public static void main(String [] args) {
Son son = new Son(); Father father ; father = (Father)son; }
}interface Father {}编译成功。为什么会这样呢?
是可以的,你可以试试。 public class Son implements Father{ public static void main(String [] args) {
Son son = new Son(); Father father ; father = (Father)son; }
你看了链接了吗http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.5Java Language Specification 第三版
楼主测试一下这段代码,final class是不可以的public final class Son{ public static void main(String [] args) { Son son = new Son(); Father father ; father = (Father)son; } } interface Father{}
5.1.6 Narrowing Reference ConversionsFrom any class type C to any non-parameterized interface type K, provided that C is not final and does not implement K.
这和final本事不可以被继承有关系,所以也不存在转型的问题了,个人的看法
无关!!public final class Son implements Father2{ public static void main(String [] args) { Son son = new Son(); Father2 f2 = (Father2)son; System.out.println("convert to father2 ok"); //Father father ; //father = (Father)son; } } interface Father{} interface Father2{}
我的意思是你的源代码是向上转型,final class是可以的
原文是 5.1.6 Narrowing Reference Conversions The following conversions are called the narrowing reference conversions : * From any class type C to any non-parameterized interface type K, provided that C is not final and does not implement K.
这也只是说明了什么是下转型的问题啊。 我想问的不是这个意思
我在看Sybex.Complete.Java.2.Certification.Study.Guide.Apr.2005 的时候 有如下几句话:For the exam, the important rules to remember when casting from Oldtype to Newtype are as follows: 1.When both Oldtype and Newtype are classes, one class must be a subclass of the other. 2.When both Oldtype and Newtype are arrays, both arrays must contain reference types (not primitives), and it must be legal to cast an element of Oldtype to an element of Newtype. 3.You can always cast between an interface and a nonfinal object.我想问的是第3句为什么编译器允许这样?(在编译时)
原先的代码,可以通过编译: package Test;public class Son{ public static void main(String [] args) { Son son = new Son(); Father f2 = (Father)son; } } interface Father{} 注意 Son没有implements Father 但是可以编译成功。 我不考虑执行的问题!final不final和我这个问题没有什么关系。 而且上面也明确说明了,You can always cast between an interface and a nonfinal object. 注意是nonfinal,not a final object。
编译器为什么允许这样?因为Java Language Specificiation规定了要这样做,所有的标准编译器就要这样实现
Father f2 = (Father)son; 上转型成接口的引用,是为了实现多态。 但是实例化一个接口是不可能的。
Interface i = new Class();这个格式?
下面以Son类继承Father类为例,其中必须有继承关系:
public class Son extends Father{ public static void main(String [] args) {
Son son = new Son();
Father father ;
father = (Father)son;
}
}class Father {}
但是为什么interface没有此限制?下面把Father类改为接口,并且Son不去implements它public class Son { public static void main(String [] args) {
Son son = new Son();
Father father ;
father = (Father)son;
}
}interface Father {}编译成功。为什么会这样呢?
public class Son implements Father{ public static void main(String [] args) {
Son son = new Son();
Father father ;
father = (Father)son;
}
}interface Father {}
Father接口 和Son类没有一点关系,怎么这样转型?
第一个例子可以的原因就是Son继承了Father~是多态的形式
Exception in thread "main" java.lang.ClassCastException: Son
ClassCastException是类型进行转换时抛出的异常
为什么编译可以通过?
莫非是编译器进行语法检查顺序的原因 ?
是不是这类错误是在运行时检查的?关注中...
而且一个类如没有实现这个接口的话,这个接口是不能引用这个类的。
“编译通过了”看是不是你的编译器在语法检查上是否有问题
顺便请教一下:
我朋友开了一家网店!开了一段时间,可没有多少人去买!我对经商不懂!如果您有空,请帮忙提点建议!非常感谢!她的网址是:http://shop35772943.taobao.com 再次感谢!
不过有几点问题,我还是说明一下。
首先,Son类的reference并不是强制转型,Father是Son的父类,也就是说Son可以自动转型成Father类(或者是他继承的接口,我之前写的(Father)是为了清楚的表示一下,不写也可以)然而Father转成Son的时候,才是需要强制转换的。就现在的情况,Son转成Father是完全没问题的,即Fahter的引用指向new Son()在栈中开辟的内存地址(我不太清楚,朋友说的失去的那片内存空间的地址是什么?)当然如果Son类和Father类没有继承关系,这样的转换也就不存在了。这个时候调用Fahter引用的某个方法,实际上执行的是Son类中此方法的实现(因为此时Fahter指向的栈地址是Son的)这也就是多态的表现吧。但是我想问的是,为什么interface和class之间的转换,在编译期间不需要并不需要implements的关系。注意,再次强调我说的是在编译期间,ClassCastException也就不在讨论范围了。其实,我就是想知道编译器为什么没有类和接口之前转换的check?
我只是想知道为什么编译器允许,类与接口之间没有任何实现关系,但是在编译时不会出错(不是运行期间)
你看了链接了吗http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.5Java Language Specification 第三版
Father father ;
father = (Father)son;
}
} interface Father{}
Father2 f2 = (Father2)son;
System.out.println("convert to father2 ok");
//Father father ;
//father = (Father)son;
}
} interface Father{}
interface Father2{}
5.1.6 Narrowing Reference Conversions
The following conversions are called the narrowing reference conversions : * From any class type C to any non-parameterized interface type K, provided that C is not final and does not implement K.
这也只是说明了什么是下转型的问题啊。
我想问的不是这个意思
有如下几句话:For the exam, the important rules to remember when casting from Oldtype to Newtype
are as follows:
1.When both Oldtype and Newtype are classes, one class must be a subclass of the other.
2.When both Oldtype and Newtype are arrays, both arrays must contain reference types
(not primitives), and it must be legal to cast an element of Oldtype to an element of
Newtype.
3.You can always cast between an interface and a nonfinal object.我想问的是第3句为什么编译器允许这样?(在编译时)
package Test;public class Son{ public static void main(String [] args) { Son son = new Son();
Father f2 = (Father)son;
}
} interface Father{}
注意 Son没有implements Father 但是可以编译成功。
我不考虑执行的问题!final不final和我这个问题没有什么关系。
而且上面也明确说明了,You can always cast between an interface and a nonfinal object.
注意是nonfinal,not a final object。
编译器为什么允许这样?因为Java Language Specificiation规定了要这样做,所有的标准编译器就要这样实现
上转型成接口的引用,是为了实现多态。
但是实例化一个接口是不可能的。
class是类(别说这是废话),
interface是类型(请别再使用接口这个词).当一个对象强制引用另一个对象的时候,当然一定要检查一下是不是类匹配(披着羊皮的狼),注意这是两个对象!
当用interface指向一个对象的时候,interface又不是一个对象,为什么要检查?
另外楼上的朋友,我再想请教一下。为什么接口不检查呢?虽然他不是一个类。但是至少要确保class implements interface 吧? 还有,为什么不能叫interface为接口呢?Thank u ^^