我们知道,多态是OO中很重要的一个概念,在面向对象的语言中,都有对它的支持。但在不同语言中,对它的实现是不一样的。
在java中,动态绑定是java默认的行为,除非你将方法声明为static和final方法。
在c++中,动态绑定不是c++的默认认为。要触发动态绑定,必须将方法声明为虚函数。在c++中,有覆盖虚函数机制。也就是说,派生类虚函数可以调用基类的版本。如下:
#include <iostream>;
using namespace std;class person {
public:
person() {
} virtual void run() {
cout<<"person running"<<endl;
}
};class man : public person {
public:
man() {
} void run() {
cout<<"man running"<<endl;
}
};int main() {
person *p = new man();
p->person::run();
system("pause");
return 0;
}
通过使用作用域名操作符::, 将强制调用基类中run()方法,而不是派生类中run()方法。问题来了,java中有这样的支持吗?能强制调用基类中的方法吗?大家可以看看这个代码
class baseClass {
void run() {
System.out.println("run function in base class is called");
}
}public class extendClass extends baseClass {
void run() {
System.out.println("run function in extend class is called");
} public static void main(String[] args) {
baseClass a[] = {new baseClass(), new extendClass()};
for (int i = 0; i < a.length; i++) {
a[i].run();
}
}
}
在java中,动态绑定是java默认的行为,除非你将方法声明为static和final方法。
在c++中,动态绑定不是c++的默认认为。要触发动态绑定,必须将方法声明为虚函数。在c++中,有覆盖虚函数机制。也就是说,派生类虚函数可以调用基类的版本。如下:
#include <iostream>;
using namespace std;class person {
public:
person() {
} virtual void run() {
cout<<"person running"<<endl;
}
};class man : public person {
public:
man() {
} void run() {
cout<<"man running"<<endl;
}
};int main() {
person *p = new man();
p->person::run();
system("pause");
return 0;
}
通过使用作用域名操作符::, 将强制调用基类中run()方法,而不是派生类中run()方法。问题来了,java中有这样的支持吗?能强制调用基类中的方法吗?大家可以看看这个代码
class baseClass {
void run() {
System.out.println("run function in base class is called");
}
}public class extendClass extends baseClass {
void run() {
System.out.println("run function in extend class is called");
} public static void main(String[] args) {
baseClass a[] = {new baseClass(), new extendClass()};
for (int i = 0; i < a.length; i++) {
a[i].run();
}
}
}
同意java支持这样操作的朋友,如foundercy_weng,能够写出示例代码吗?谢谢
多态的概念都一样
JAVA内建了虚函数表,他所有的方法都是virtual的
所以只要方法签名相同,子类的方法就会被认为是覆盖方法JAVA效率比C低的一大原因就是内建的虚函数表
C在不使用virtual关键字的情况,执行效率比较高
你可以让子类提供其他的方法,这个方法除了调用你要使用的父类方法之外什么都不作.
public class TestOverride { static public void main( String[] args ) {
Base o = new Derived();
o.doSomething();
}
}class Base {
public void doSomething() {
System.out.println("here is Base.doSomething()");
}
}class Derived extends Base {
public void doSomething() {
System.out.println("here is Derived.doSomething()");
super.doSomething(); // 请注意这里!
}
}
通过super,只能在类内部调用,不是通过外部实例。看来通过外部实例,在java中是无法调用被子类覆盖的父类代码,c++却可以做到。
class Animal{
public String toString()
{
return this.getClass().getName();
}
public void eat()
{
System.out.println("Animal");
}
}
class Dog extends Animal
{
public void eat()
{
System.out.println("Dog");
}
public void superEat()
{
System.out.println("super Dog eat");
super.eat();
}
}
class Tiger extends Animal
{
public void eat()
{
System.out.println("Tiger");
}
public void superEat()
{
super.eat();
}
}
class Monkey extends Animal
{
public void eat()
{
System.out.println("Monkey");
}
}
public class qdb
{ private static void factory(Dog dog)
{
//dog.eat();
dog.superEat();
}
private static void factory(Tiger tiger)
{
tiger.eat();
}
private static void factory(Animal animal)
{
animal.eat();
}
public static void main(String[] args)
{
factory(new Tiger());
factory(new Dog());
factory(new Monkey());
factory(new Animal());
}
}
</code>
利用多态模拟出来的,不过一般谁会无聊这么用法啊?