Decentralized decision-making is the opposite of centralized decision-making. The following code is written based on the idea of decentralized decision-making.
Class P2
{
Private:
Int m_nVal;
Public:
Int fun (int nVal1, int nVal2, int nVal3) const
{
If (m_nVal <60)
Return nVal1;
Else if (m_nVal = 60)
Return nVal2;
Else if (m_nVal> 60)
Return nVal3;
Return-1;
}
};
Class P1
{
Private:
Int m_nVal;
Public:
Int fun (const P2 & aP2) const
{
If (m_nVal <60)
Return aP2.fun (0, 1, 2 );
Else if (m_nVal = 60)
Return aP2.fun (3, 4, 5 );
Else if (m_nVal> 60)
Return aP2.fun (6, 7, 8 );
Return-1;
}
}; Www.2cto.com
Int fun (const P1 & aP1, const P2 & aP2)
{
Return aP1.fun (aP2 );
}
The if-else judgment in the original fun function is moved to the inside of the class, which is the object-oriented method. Each class is responsible for its internal data and makes decisions accordingly.
From acloudhuang's column