用Java导入包 [英] Importing packages in Java

查看:123
本文介绍了用Java导入包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将方法从包导入另一个程序?我不知道如何导入...我写了一个lil'代码:

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:\ Dan \ Vik.java
中,第二个代码保存在C:\\ \\ kab.java

I saved the first code in C:\Dan\Vik.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆