单继承模式下的JAVA和C++
生活随笔
收集整理的這篇文章主要介紹了
单继承模式下的JAVA和C++
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
總結(jié):單繼承模式下JAVA和C++相同,處理代碼方面和事后處理方面有點(diǎn)小差異外沒有什么本質(zhì)的不同
_Main.cpp #include "Base.h" #include "Child.h"void main() {Base *base = new Child();base->BasePrint();printf_s("\n");Child *child = new Child();child->ChildPrint();printf_s("\n");child->BasePrint();getchar();delete base;delete child; } 運(yùn)行結(jié)果:
JAVA一共3個文件
1.Base.class?
2.Child.class(繼承Base)
3._Main.class
C++一共3個文件
1.Base.h
2.Child.h(繼承Base)
3._Main.cpp
下面展示JAVA代碼
Base.class
package ABoutClass;public class Base {public int a=1;public void BasePrint(){System.out.println("Base-print Called and a is "+a);} }
下面是C++代碼
Base.h
#pragma once #include <stdio.h> class Base { public:int a;Base() :a(1){}void BasePrint(){printf_s("Base-print Called and a is %d", a);} };
_Main.cpp #include "Base.h" #include "Child.h"void main() {Base *base = new Child();base->BasePrint();printf_s("\n");Child *child = new Child();child->ChildPrint();printf_s("\n");child->BasePrint();getchar();delete base;delete child; } 運(yùn)行結(jié)果:
下面對比下:
發(fā)現(xiàn)在單繼承中,JAVA和C++并沒有什么不同,但JAVA不支持多繼承(避免菱形問題)
但JAVA中接口(interface)來指明多個類實現(xiàn)的方法(方法和C++里面的函數(shù)等同)
總結(jié)
以上是生活随笔為你收集整理的单继承模式下的JAVA和C++的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt creator5.7 OpenCV
- 下一篇: C/C++轻松写基于UDP的远程控制