我收到警告:
RS232Msg 无法响应 "-initWithRS232MsgRawEncoded">
代码是
-(void)createMessage
{
RS232Msg* pMsg;
//pMsg = new RS232MsgRawEncoded(static_cast<int>nMessageNumber); in cpp
pMsg = [pMsg initWithRS232MsgRawEncoded:(int)nMessageNumber];
}
initWithRS232MsgRawEncoded
是RS232Msg
的派生类。pMsg
是指向RS232Msg
的指针。创建消息是在RS232Msg中声明的方法如何使其访问?
如果在派生自RS232Msg
的类中定义了initWithRS232MsgRawEncoded
,则不能将该选择器与RS232Msg*
一起使用。
如果我正确理解您要做什么,您希望通过使用原始编码初始化它们来添加另一种创建RS232Msg
对象的可能性。
您可以通过不同的方式做到这一点。一种是创建一种"工厂"类(根据GoF模式,它不会是正统的工厂,但这并不重要(。此类可以具有一个完全是您的initWithRS232MsgRawEncoded
的静态函数。
您拥有的另一个选项是为RS232
定义一个类别,然后将initWithRS232MsgRawEncoded
添加到其中。类别是一种扩展类而无需对其进行子类化的方法。这是您在情况下如何做到这一点的骨架:
@interface RS232 (MyRS232Extension)
(id)initWithRS232MsgRawEncoded:....;
@end
@implementationRS232 (MyRS232Extension)
....
@end