将 Java 转换为目标 C

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18407633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:21:07  来源:igfitidea点击:

Converting Java to objective C

javaobjective-cconverter

提问by Siddharthan Asokan

I'm trying to convert a Java code to Objective-C. The class below is the extension of TestCodeRequest. Wondering how to convert this to an objective C equivalent. I'm finding it a bit confusing because Java is statically typed, and configuration over convention. Whereas Objective-C is dynamically typed, and convention over configuration. I have a sample code below. Any small hint should be really great.

我正在尝试将 Java 代码转换为 Objective-C。下面的类是 TestCodeRequest 的扩展。想知道如何将其转换为客观的 C 等效项。我发现它有点令人困惑,因为 Java 是静态类型的,并且配置高于约定。而 Objective-C 是动态类型的,并且约定优于配置。我在下面有一个示例代码。任何小提示都应该很棒。

package com.TestCode.api;

import java.io.IOException;

import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;

import org.json.JSONObject;

public class Categories extends TestCodeRequest {

    public Categories(String apiKey, String apiSecret) {
            super(apiKey, apiSecret, "categories");
    }

    public Categories field(Object... fields) {
        super.field(fields);
        return this;
    }

    public JSONObject getCategories() throws OAuthMessageSignerException, OAuthExpectationFailedException, OAuthCommunicationException, IOException {
        return this.get();
    }

    public Categories categoriesField(Object... fields) {
        return this.field(fields);
    }

}

采纳答案by Mario Rossi

There are several good Java to Objective C translators. For code thatsimple, all of them should work. Where translators usually have a lot of trouble is translating calls to runtime libraries because of their divergent philosophies (especially in the area of look-and-feel and user interaction). To do a good job here, they need to go from syntax back to semantics, and this is extremely difficult for software to do.

有几个很好的 Java 到 Objective C 的翻译器。对于这么简单的代码它们都应该可以工作。翻译人员通常遇到很多麻烦的地方是翻译对运行时库的调用,因为他们的理念不同(尤其是在外观和用户交互方面)。要在这里做好,他们需要从语法回到语义,而这对于软件来说是极其困难的。

Your phrase "Java is statically typed, and configuration over convention. Whereas Objective-C is dynamically typed, and convention over configuration" can't but refer to this. Specifically GUI development. This aspect of the languages is not intrinsic to them, but belongs to the libraries that developers usually employ, in many cases dependent on specific Operating Systems. So, we are probably not talking as much about converting Java to Objective C as of converting from a Swing look-and-feel and mode of interaction to an iOS one.

您的短语“Java 是静态类型的,并且配置高于约定。而 Objective-C 是动态类型的,并且约定高于配置”不能不提到这一点。特别是GUI开发。语言的这一方面不是它们固有的,而是属于开发人员通常使用的库,在许多情况下取决于特定的操作系统。因此,我们可能不会像从 Swing 外观和交互模式转换为 iOS 那样谈论将 Java 转换为 Objective C。

All considered, I'd recommend you to use automatic conversion tools as learning tools. You will see that the generated pieces of code (as they become more complex and/or incorporate user interfaces) even if they work, are not suitable for maintenance or further development and need to be re-coded if not redesigned. But again, as learning tools they are very useful.

综合考虑,我建议您使用自动转换工具作为学习工具。您将看到生成的代码段(随着它们变得更加复杂和/或包含用户界面)即使它们有效,也不适合维护或进一步开发,如果不重新设计则需要重新编码。但同样,作为学习工具,它们非常有用。

  • Google J2ObjC
  • java2objc
  • JCGOThis one converts to C. If the purpose is to have something working, this could be good enough. Not if the purpose is to learn Objective C in all its splendor.
  • 谷歌 J2ObjC
  • java2objc
  • JCGO这一个转换为 C。如果目的是让某些东西工作,这可能已经足够了。如果目的是学习 Objective C 的所有辉煌,那就不是了。