I’m trying to convert an Objective C code to Java,but I have a little problem with understanding how to do it.
Here is the Objective C code:
+ (StPacket *)initialize:(UInt32)aPacketId packetType:(StPacketType)aPacketType operationType:(StOperationType)aOperationType
objectOid:(NSString*)aObjectOid objectId:(UInt32)aObjectId dataSize:(UInt32)aDataSize dataHash:(NSString*)aDataHash
dataType:(StPacketDataType)aDataType packetData:(NSData*)aPacketData {
StPacket * packet = nil;
switch (aPacketType)
{
// Special packets
case ST_OBJECT_TYPE_INFO_START:
{
packet = [[StPacketInfoStart alloc] init];
break;
}
case ST_OBJECT_TYPE_INFO_END:
{
packet = [[StPacketInfoEnd alloc] init];
break;
}
}
if (packet)
{
[packet setPacketData:aPacketId packetType:aPacketType operationType:aOperationType objectOid:aObjectOid objectId:aObjectId
dataSize:aDataSize dataHash:aDataHash dataType:aDataType packetData:aPacketData];
}
return [packet autorelease];
}
StPacketInfoStart init :
- (id)init {
self = [super init];
if (self)
{
locale = nil;
serverApiVer = nil;
deviceId = 0;
}
return self;
}
So I did it in Java like this :
public RPCPacket( int apacketId,
RPCPacketType apacketType,
RPCOperationType aoperationType,
String[] aobjectOid,
int aobjectId,
int adataSize,
String[] adataHash,
RPCPacketDataType adataType){
RPCPacket packet=null;
switch(apacketType){
case ST_OBJECT_TYPE_INFO_START:
{
packet = new InfoStartRPCPacket();
break;
}
case ST_OBJECT_TYPE_INFO_END:
{
packet = new InfoEndRPCPacket();
break;
}
}
packet = new RPCPacket(adataSize, apacketType, aoperationType, adataHash, adataSize, adataSize, adataHash, adataType);
}
My question is,is that the right way and if the code in Java is doing the same thing as in Objective C,because now I’m getting an error which is sayin “Implicit super constructor RPCPacket() is undefined for default constructor. Must define an explicit constructor”.
Any ideas?
I don’t talk Objective-C, but I’m pretty sure you don’t do the same thing, as the Java code is not correct:
From the error message (and the name) I take it that your
RPCPacket“method” is a constructor. That’s fine, if you want it to be called whenever someone usesnew RPCPacket()with the appropriate arguments.But at the end of your constructor your *create a new
RPCPacketobject which is almost always an error: there already is a newRPCPacketobject while the constructor is running. It can be accessed usingthis(or implictly) and it needs to be initialized by that code.So instead of trying to create a new
RPCPacketobject in that method, you should simply set the fields to the appropriate values.