a*********e 发帖数: 697 | 1 看书时好像没注意,忽然觉得不明白了。
superclass
class A {
array m_arry[];
init(array); //to init m_arry
}
subclass
class B {
sort(this.m_arry);
}
main(){
A a;
B b=(B)a;
b.sort();
}
这样写把super class 对象downcast成subclass 的对象是会有问题的。调用b.sort()
是会出错,还是不出错但sort只是作用在b自己的m_arry上? 如果写:
b.init(a.m_arry);
b.sort();
是否就可以了? |
s*******d 发帖数: 59 | 2 this downcast will raise exception |
L*********r 发帖数: 92 | 3 Downcast is related to object instance.
You can do it as follwoing:
A a = new B;
B b = (B)a;
b.sort;
【在 a*********e 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 看书时好像没注意,忽然觉得不明白了。 : superclass : class A { : array m_arry[]; : init(array); //to init m_arry : } : subclass : class B { : sort(this.m_arry); : }
|
e*****t 发帖数: 1 | 4 Downcast is for Compile-time type check, i.e., for the compilers to do error
checking. For example, if you declare a class A and then point it to a Run-
time type B like the following,
Class A{
A(); //ctor
int a;
}
Class B extends A{
super();
sort();
}
A a = new B();
You can not say
a.sort(); //compile error
But you can say
(B)a.sort(); //compile OK
====
if you say
A a = new A();
B b = a;
you will get a compile error.
=======
if you say
A a = new A();
B b = new B();
a = b; // this is OK.
a.sort |
g*****g 发帖数: 34805 | 5
error
Run-
will have compile error, ((B)a).sort() is fine,
【在 e*****t 的大作中提到】![](/moin_static193/solenoid/img/up.png) : Downcast is for Compile-time type check, i.e., for the compilers to do error : checking. For example, if you declare a class A and then point it to a Run- : time type B like the following, : Class A{ : A(); //ctor : int a; : } : Class B extends A{ : super(); : sort();
|
L*********r 发帖数: 92 | 6 你应该理解一下下面的rules in java.
1.The rules for a casting from compile-time reference type S to a compile-
time reference type T are as follows:
If S is a class type:
If T is a class type, then either |S| <: |T|, or |T| <: |S|; otherwise a
compile-time error occurs.
2.
If a cast to a reference type is not a compile-time error, there are several
cases:
The cast is a checked cast. Such a cast requires a run-time validity check.
downcast follows the above 2 rules.
error
Run-
【在 e*****t 的大作中提到】![](/moin_static193/solenoid/img/up.png) : Downcast is for Compile-time type check, i.e., for the compilers to do error : checking. For example, if you declare a class A and then point it to a Run- : time type B like the following, : Class A{ : A(); //ctor : int a; : } : Class B extends A{ : super(); : sort();
|
a*********e 发帖数: 697 | 7 是的,我在编译器里自己写了一遍结果就出现exception了。
多谢各位指点!
还有一点疑问,如果我已经有了A的对象a,并且已经初始化了m_arrary,
A a=new A();
a.m_array=...;
我想再定义一个有sort功能的扩展类B的对象b,并让它具有a中的m_array值该怎么操作?
这时是不能用的:
B b=(B)a;
也不能重新定义:
A a=new B();
最好的实现方法是不是定义一个B的constructor,用A类型对象作为参数,把m_array的
元素一一复制一遍?
B (A a){
this.m_array[]=a.m_array[];
}
再调用:
B b=new B(a); |
g*****g 发帖数: 34805 | 8 Either make m_array protected, or make it private
and have a getter. BTW, m_array is not naming convention for java.
作?
【在 a*********e 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 是的,我在编译器里自己写了一遍结果就出现exception了。 : 多谢各位指点! : 还有一点疑问,如果我已经有了A的对象a,并且已经初始化了m_arrary, : A a=new A(); : a.m_array=...; : 我想再定义一个有sort功能的扩展类B的对象b,并让它具有a中的m_array值该怎么操作? : 这时是不能用的: : B b=(B)a; : 也不能重新定义: : A a=new B();
|
a*********e 发帖数: 697 | 9 Yes, I know the unstandard naming.
sorry, I didn't get you. What's the matter of "private" or "protect" with
using B b's member array and sorting?
【在 g*****g 的大作中提到】![](/moin_static193/solenoid/img/up.png) : Either make m_array protected, or make it private : and have a getter. BTW, m_array is not naming convention for java. : : 作?
|