为什么我不能从方法声明中单独导入?(java)

why can't I import separately from the method declaration?(java)

我编写了一个简单的程序来解码我拥有的 base64 编码字符串。我使用 eclipse 搜索了一种方法来执行此操作,并发现 javax.xml.bind.DatatypeConverter.parseBase64Binary(String s) 方法正是这样做的。我发现当我使用该方法的完整位置时,程序运行正常:

public static void main(String args[]) {
String s = "cGFzc3dvcmQ6IGlsb3ZlbXlzZWxmISEx";
byte[] converted = javax.xml.bind.DatatypeConverter.parseBase64Binary(s);
System.out.println(new String(converted));
}

但出于某种原因,当我尝试导入位置时,eclipse 给我一个错误:

进口:

import javax.xml.bind.DatatypeConverter.*;

第一个代码中的新行 3:

 byte[] converted = javax.xml.bind.DatatypeConverter.parseBase64Binary(s);

错误,我进入新的第 3 行:

 The method parseBase64Binary(String) is undefined for the type **name of class**

我很乐意提供解释。

您需要执行 static 导入:

import static javax.xml.bind.DatatypeConverter.*;

import static javax.xml.bind.DatatypeConverter.parseBase64Binary;

然后你将能够做到:

byte[] converted = parseBase64Binary(s);

更多信息:

删除 javax.xml.bind.DatatypeConverter.*; 中的 .*:

import javax.xml.bind.DatatypeConverter;

public class Test {

    public static void main(String[] args)
    {
        String s = "cGFzc3dvcmQ6IGlsb3ZlbXlzZWxmISEx";
        byte[] converted = DatatypeConverter.parseBase64Binary(s);
        System.out.println(new String(converted));
    }
}
import static javax.xml.bind.DatatypeConverter.*;

然后-

byte[] converted = parseBase64Binary(s);