小编ivi*_*viO的帖子

多态性?C++与Java

尝试将多态性作为初学者.我想我正在尝试使用不同语言的相同代码,但结果并不相同:

C++

#include <iostream>
using namespace std;

class A {
    public:

    void whereami() {
        cout << "You're in A" << endl;
    }
};

class B : public A {
    public:

    void whereami() {
        cout << "You're in B" << endl;
    }
};

int main(int argc, char** argv) {
    A a;
    B b;
    a.whereami();
    b.whereami();
    A* c = new B();
    c->whereami();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

结果:

You're in A
You're in B
You're in A
Run Code Online (Sandbox Code Playgroud)

Java:

public class B extends A{

    void …
Run Code Online (Sandbox Code Playgroud)

c++ java polymorphism comparison

5
推荐指数
2
解决办法
1837
查看次数

标签 统计

c++ ×1

comparison ×1

java ×1

polymorphism ×1