简体   繁体   English

使用jython在Python中实例化与Java相同的对象

[英]Instantiating object in Python same as Java using jython

In Java I was able to run my code as: (This are just sample naming) Java中,我能够按以下方式运行代码:(这只是示例命名)

import com.projectname.api.APIOne;
import com.projectname.api.APITwo;
import com.projectname.api.APIThree;
import com.projectname.api.APIFour;

import com.projectname.api.MainAPI;

public class TestMain {

    public static void main(String[] args) {

        APIOne a = APIOne.getName();
        APITwo b = APIThree.getAddress();
        APIFour d = b.getEmail();

        MainAPI mainapi = new MainAPI();
        mainapi.setEmail(d)
    }
}

It is running okay, I tried converting this to Python as: 运行正常,我尝试将其转换为Python

import com.projectname.api.APIOne as APIOne;
import com.projectname.api.APITwo as APITwo;
import com.projectname.api.APIThree as APIThree;
import com.projectname.api.APIFour as APIFour;

def test():

    a = APIOne.getName();
    b = APIThree.getAddress();
    d = b.getEmail();

    mainapi = MainAPI();
    mainapi.setEmail(d)

test()

But is this the right way of instantiating? 但这是正确的实例化方法吗? It make me confuse on instantiating. 它使我对实例化感到困惑。

Hope you could help me. 希望你能帮助我。

我不明白为什么会感到困惑,但这是正确的,您可以检查有关使用Jython实例化Java对象并以与您相同的方式实例化对象的Jython文档

Importing a class from a java package or python module is normally written as: 从Java包或python模块导入类通常写为:

from java.lang import Math

Rather than: 而不是:

import java.lang.Math as Math

But, your code is correct. 但是,您的代码是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM