那么,Java真的就不能调用父类已经被覆盖的方法了吗?
如果不能调用。更进一步,我发现C++可以很方便地调用父类的被覆盖的方法。代码如下。那么,从设计上,为什么C++可以允许这样调,而Java不能
呢?
#include <stdio.h>
class A
{
public:
virtual void say() {
printf("in function A\n");
}
};
class B : public A
{
public:
virtual void say() {
printf("in function B\n");
}
};
int main() {
A * a = new B();
a->say();
a->A::say();
getchar();
return 0;
}
应该是因为java类全都是虚函数的关系。
c++是通过显示指定一个父类方法(A::xxx)达到调用目的,如果也是虚函数的话,就和java一样效果。
不知道java能不能显示指定父类方法。
2009/6/22 Jay True <gla...@gmail.com>:
All method invocation in Java is done polymorphically--based on the
true type of the instance, not on the declared type of the variable or
value through which the invocation is made. Casting changes only the
static or compile-time type associated with a value, not the real type
of the instance to which the variable or value refers.
On Jun 22, 10:54 am, cdredfox <cdred...@gmail.com> wrote:
> java 是可以调用父类的被覆盖的方法的。用关键字super
>
> 2009/6/22 Jay True <glac...@gmail.com>:
>
>
>
> > super ,吧
>
> > 2009/6/22 Yu-zhong shen <slaviks...@gmail.com>
>
> >> 应该是因为java类全都是虚函数的关系。
>
> >> c++是通过显示指定一个父类方法(A::xxx)达到调用目的,如果也是虚函数的话,就和java一样效果。
>
> >> 不知道java能不能显示指定父类方法。
>
> > --
> > 歌词唱清楚,不是周杰伦
> > Home Page:http://glacjay.is-a-geek.org/blog/- Hide quoted text -
>
> - Show quoted text -
On Jun 22, 12:38 pm, Alan Gao <cgao...@gmail.com> wrote:
> 再接着我之前的回复说说我的想法。
> 因为Java是一种严格的面向对象的语言,而C++想对来说比较灵活,严格地说,C++的一些特点打破了一些OO语言的逻辑性。细想想看,Java不允许从外部-防问父类被覆盖的方法是有其逻辑性的,举个例子:
应该是因为java类全都是虚函数的关系。
c++是通过显示指定一个父类方法(A::xxx)达到调用目的,如果也是虚函数的话,就和java一样效果。
不知道java能不能显示指定父类方法。