同样的代码在java和c++中结果不同

 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,加上后均为子类

转载于:https://www.cnblogs.com/luxishi/p/5665175.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值