简体   繁体   English

在Java中导入一个自定义的class

[英]Import a custom class in Java

How do I import a class I wrote in a different file?如何导入我在不同文件中写入的 class? All my classes are under the same package.我所有的课程都在同一个 package 下。

If all of your classes are in the same package, you shouldn't need to import them. 如果所有类都在同一个包中,则不需要导入它们。

Simply instantiate the object like so: 只需像这样实例化对象:

CustomObject myObject = new CustomObject();

Import by using the import keyword: 使用import关键字import

import package.myclass;

But since it's the default package and same , you just create a new instance like: 但由于它是默认包并且相同 ,因此您只需创建一个新实例:

elf ob = new elf(); //Instance of elf class

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. 因此您不必导入,可以创建没有导入语句的新实例。

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 . EclipseNetBeans中,只需编写要使用的类,然后按Ctrl + Space The IDE will automatically import the class. IDE将自动导入该类。

General information: 一般信息:

You can import a class with import keyword after package information: 您可以在包信息后导入带有import关键字的类:

Example: 例:

package your_package;


import anotherpackage.anotherclass;

public class Your_Class {
    ...
    private Vector variable;
    ...
}

You can instance the class with: 您可以使用以下实例来实例:

Anotherclass foo = new Anotherclass();

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) 要在类A中调用B类中的方法,您应该使用classB.methodName(arg)

According Oracle and Sun doc, a class can use all classes from its own package and all public classes from other packages. 根据Oracle和Sun doc,类可以使用其自己的包中的所有类以及其他包中的所有公共类。 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(); java.util.Date today = new java.util.Date();

  • The simpler, and more common, approach is to use the import statement. 更简单,更常见的方法是使用import语句。 The point of the import statement is to give you a shorthand to refer to the classes in the package. import语句的目的是为您提供一个简写来引用包中的类。 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). 将import语句放在源文件的顶部(但在任何包语句下面)。 For example, you can import all classes in the java.util package with the statement Then you can use without a package prefix. 例如,您可以使用语句导入java.util包中的所有类。然后您可以在没有包前缀的情况下使用。

    import java.util.*; import java.util。*;

    // Use class in your code with this manner //以这种方式在代码中使用class

    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. 正如您在问题中提到的,您的类在同一个包中,您应该没有任何问题,最好只使用类名。

If you have the java files under the same package, you will not need to import the classes within those java files.如果在同一个 package 下有 java 个文件,则不需要导入这些 java 文件中的类。 Simply instantiate an object and you will have access.只需实例化一个 object 即可访问。 For example, if you need to access class named My_Class from another file within the same package:例如,如果您需要从同一个 package 中的另一个文件访问名为 My_Class 的 class:

My_Class object = new My_Class();

Now, if you put a .现在,如果你放一个. dot after typing object, you will gain access to all the methods and attributes from the class.输入 object 后点,您将可以访问 class 中的所有方法和属性。

Now, if you don't have the files under the same package, you can import the Class. Just type:现在,如果您没有相同 package 下的文件,您可以导入 Class。只需键入:

import Package_Name.Class_name

Where Package_Name represents the name of the package in which the class exists and Class_name being the name of the class you want to import.其中Package_Name表示class所在的Class_name的名字,Class_name是你要导入的class的名字。

First off, avoid using the default package. 首先,避免使用默认包。

Second of all, you don't need to import the class; 其次,您不需要导入该类; it's in the same package. 它在同一个包装中。

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

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