1 #include <iostream> 2 using namespace std; 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 4 class A { 5 public: 6 void m() { 7 n(); 8 } 9 10 public: 11 virtual void n() { 12 cout<<"调用了父类的n函数"; 13 } 14 15 }; 16 17 class B:public A { 18 19 public: 20 void m() { 21 A::m(); 22 } 23 24 public: 25 void n() { 26 cout<<"调用了子类的n函数"; 27 } 28 29 }; 30 int main(int argc, char** argv) { 31 B b; 32 b.m(); 33 return 0; 34 }
class A { public void m() { n(); } public void n() { System.out.println("调用了父类的n函数"); } } class B extends A { public void m() { super.m(); } public void n() { System.out.println("调用了子类的n函数"); } } public class main { public static void main(String[] args) { // TODO Auto-generated method stub B b=new B(); b.m(); } }
c++输出父类,java输出子类。
原因java默认多态,动态绑定。c++需要加virtual,加上后均为子类
时间: 2024-09-29 07:38:33