zoukankan      html  css  js  c++  java
  • 同样的代码在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,加上后均为子类

  • 相关阅读:
    位置匹配
    匹配重复
    使用元字符
    匹配一组字符
    匹配任意单个字符
    python-全局替换程序
    python37-encode与decode
    python37-能检测文件编码的模块
    super方法
    类-易错题
  • 原文地址:https://www.cnblogs.com/luxishi/p/5665175.html
Copyright © 2011-2022 走看看