在 Java 中导入包 [英] Importing packages in Java
问题描述
如何将包中的方法导入到另一个程序中?我不知道如何导入...我写了一个小代码:
How to import a method from a package into another program? I don't know how to import... I write a lil' code:
package Dan;
public class Vik
{
public void disp()
{
System.out.println("Heyya!");
}
}
然后,将其保存在名为Dan"的文件夹中并进行编译.生成 .class 文件.然后,我在下面写了这段代码:
and then, saved it in a folder named "Dan" and I compiled it. The .class file is generated. Then, I wrote this code below:
import Dan.Vik.disp;
class Kab
{
public static void main(String args[])
{
Vik Sam = new Vik();
Sam.disp();
}
}
我把它保存在文件夹Dan"之外,上面写着:找不到符号"
and I saved it outside the folder "Dan" and it says : "cannot find symbol"
我将第一个代码保存在 C:DanVik.java第二个在 C:Kab.java
I saved the first code in C:DanVik.java and the second in C:Kab.java
推荐答案
Java 中不导入方法,只导入类型:
You don't import methods in Java, only types:
import Dan.Vik;
class Kab
{
public static void main(String args[])
{
Vik Sam = new Vik();
Sam.disp();
}
}
例外是所谓的静态导入",它允许您从其他类型导入类(static
)方法.
The exception is so-called "static imports", which let you import class (static
) methods from other types.
这篇关于在 Java 中导入包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!