c 子類物件呼叫父類的方法

2021-10-21 11:47:05 字數 1127 閱讀 3185

第一種方式就是子類重寫父類的方法,在子類中定義和父類同樣的方法,方法體中呼叫父類的方法

父類**

#pragma once

#include "cclientsocket.h"

class ctcpinte***ce

;

#include "ctcpinte***ce.h"

#include #include #include "../utils/ccommon.h"

ctcpinte***ce::ctcpinte***ce()

ctcpinte***ce::~ctcpinte***ce()

bool ctcpinte***ce::connect(cstring ip, int port)

catch (const std::exception&)

return b_result;

}bool ctcpinte***ce::sendcmd(cstring cmd)

catch (const std::exception&)

return b_result;

}

子類**

#pragma once

#include "cclientsocket.h"

#include "ctcpinte***ce.h"

class __declspec(dllexport) cplcservice:public ctcpinte***ce

;

#include "cplcservice.h"

#include #include #include "../utils/ccommon.h"

cplcservice::cplcservice()

cplcservice::~cplcservice()

bool cplcservice::connect(cstring ip, int port)

bool cplcservice::sendcmd(cstring cmd)

第二種方法

在使用的地方定義基類的指標,指向new的子類,需要用子類時候,再將父類指標強轉為子類的指標

物件導向之子類呼叫父類的方法

在子類中呼叫父類的方法 class vehicle1 country china def init self,name,speed,load,power self.name name self.speed speed self.load load self.power power defrun se...

Python 子類呼叫父類方法

python在繼承時,如果子類重寫了init 函式,則父類的init 不會被呼叫,這時如果子類只是想要對父類的init 函式進行簡單的擴充套件的話,是很不方便的。那麼有沒有比較方便的方法來從子類呼叫父類呢?第一種是直接使用父類的類名來直接呼叫。python3.3 class parent def i...

python子類呼叫父類的方法

情況一 子類需要自動呼叫父類的方法 子類不重寫 init 方法,例項化子類後,會自動呼叫父類的 init 的方法。class father object def init self,name self.name name print name s self.name def getname self...