码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-07-12 23:12:22      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

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

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

标签:

原文地址:http://www.cnblogs.com/luxishi/p/5665175.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!