在 Java 中导入自定义类
如何导入我在不同文件中编写的类?我的所有课程都在同一个包下。
How do I import a class I wrote in a different file? All my classes are under the same package.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果所有类都在同一个包中,则不需要导入它们。
只需像这样实例化对象:
If all of your classes are in the same package, you shouldn't need to import them.
Simply instantiate the object like so:
使用
import
关键字导入:但由于它是默认包并且相同,您只需创建一个新实例,例如:
Import by using the
import
keyword:But since it's the default package and same, you just create a new instance like:
在同一个包中,您不需要导入该类。
否则的话,是很容易的。在 Eclipse 或 NetBeans 只需编写您要使用的类,然后按 Ctrl + Space。 IDE 将自动导入该类。
一般信息:
您可以在包信息后使用 import 关键字导入一个类:
示例:
您可以使用以下方法实例化该类:
In the same package you don't need to import the class.
Otherwise, it is very easy. In Eclipse or NetBeans just write the class you want to use and press on Ctrl + Space. The IDE will automatically import the class.
General information:
You can import a class with import keyword after package information:
Example:
You can instance the class with:
我看到图片了,你所有的类都在同一个包中。所以你不必导入,你可以创建一个新的实例,不需要导入语句。
I see the picture, and all your classes are in the same package. So you don't have to import, you can create a new instance without the import sentence.
首先,避免使用默认包。
其次,您不需要导入该类;它在同一个包中。
First off, avoid using the default package.
Second of all, you don't need to import the class; it's in the same package.
如果您的类位于同一个包中,则无需导入。要在 A 类中调用 B 类的方法,您应该使用
classB.methodName(arg)
If your classes are in the same package, you won't need to import. To call a method from class B in class A, you should use
classB.methodName(arg)
根据 Oracle 和 Sun 文档,一个类可以使用它自己的包中的所有类以及其他包中的所有公共类。您可以通过两种方式访问另一个包中的公共类。
第一个方法是在每个类名前面添加完整的包名称。例如:
java.util.Date Today = new java.util.Date();
更简单、更常见的方法是使用 import 语句。 import 语句的目的是为您提供引用包中的类的简写。一旦使用导入,您就不再需要为类提供全名。您可以导入特定的类或整个包。您将导入语句放置在源文件的顶部(但位于任何包语句下方)。例如,您可以使用以下语句导入 java.util 包中的所有类,然后您可以在没有包前缀的情况下使用。
导入java.util.*;
// 以这种方式在代码中使用类
Date Today = new Date();
正如您在问题中提到的,您的类位于同一个包下,您不应该有任何问题,最好只使用类名。
According Oracle and Sun doc, a class can use all classes from its own package and all public classes from other packages. You can access the public classes in another package in two ways.
The first is simply to add the full package name in front of every class name. For example:
java.util.Date today = new java.util.Date();
The simpler, and more common, approach is to use the import statement. The point of the import statement is to give you a shorthand to refer to the classes in the package. Once you use import, you no longer have to give the classes their full names. You can import a specific class or the whole package. You place import statements at the top of your source files (but below any package statements). For example, you can import all classes in the java.util package with the statement Then you can use without a package prefix.
import java.util.*;
// Use class in your code with this manner
Date today = new Date();
As you mentioned in your question that your classes are under the same package, you should not have any problem, it is better just to use class name.
如果您的 java 文件位于同一包下,则无需导入这些 java 文件中的类。只需实例化一个对象,您就可以访问。例如,如果您需要从同一包中的另一个文件访问名为 My_Class 的类:
现在,如果您在键入 object 后放置
.
点,您将获得对该包中的所有方法和属性的访问权限。班级。现在,如果同一个包下没有这些文件,您可以导入该类。
只需键入:
其中
Package_Name
表示该类所在的包的名称,并且Class_name
是您要导入的类的名称。If you have the java files under the same package, you will not need to import the classes within those java files. Simply instantiate an object and you will have access. For example, if you need to access class named My_Class from another file within the same package:
Now, if you put a
.
dot after typing object, you will gain access to all the methods and attributes from the class.Now, if you don't have the files under the same package, you can import the Class.
Just type:
Where
Package_Name
represents the name of the package in which the class exists andClass_name
being the name of the class you want to import.