开发者

Little issue with converting Objective C to Java Android

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开发者_C百科 {

    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 uses new RPCPacket() with the appropriate arguments.

But at the end of your constructor your *create a new RPCPacket object which is almost always an error: there already is a new RPCPacket object while the constructor is running. It can be accessed using this (or implictly) and it needs to be initialized by that code.

So instead of trying to create a new RPCPacket object in that method, you should simply set the fields to the appropriate values.


This code is the code of a RTCPacket constructor. Did you write it as being a method of the RPCPacket class?

On top of this:

  • You are defining another RPCPacket by writing RPCPacket packet=null;, then you set packet according to a switch statement, but you use packet nowhere else in the code. As packet is a local variable, it is then lost. What is the use for packet, then?

  • You are calling super(...) with the arguments passed to RTCPacket(...). But what is the superclass of RPCPacket? This may be the cause of the error: Constructor call must be the first statement in a constructor (this message is a copy/paste of javac's error message when trying to use super after having written some other code in the constructor).

EDIT, further to comments below:

InfoStartRPCPacket and EndStartRPCPacket seem to be subclasses of RPCPacket. But you are not defining the type of your instance the right way. Instead of writing a long text here, I suggest you visit this tutorial that explains the Java constructors better than I would do.


Do yourself a favor and implement the Android incarnation from the ground up. I've seen the results of "translating" ObjC into Java, and it's not good. They're two different beasts, especially if you're not familiar with the nuances of each.

Use the UI you want to create as a starting point, design your code with Java in mind, and use your ObjC only as a reference at the overall architecture level. If you do that, the logic should fall into place naturally.

Again, do not translate line by line. A line of Java may look like a line of ObjC, but could do something very, very different that you only find out about too late.

Good luck.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜