在Java中访问其他类文件 [英] Accessing other class files in Java

查看:128
本文介绍了在Java中访问其他类文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们刚刚开始学习Java的学位课程,并且我已经获得了一个包含各种Java类的文件夹,每个都在自己的.java文件中,文件名与它所拥有的类的名称相同in。

We've just started learning Java for my degree and I've been given a folder with various Java classes in, each in their own .java file, with the file name the same as the name of the class it has it in.

有一个文件托管一个公共类,其中包含以下内容:

There is one file which hosts a public class which has the following in:

public static void main(String[] args) {}

存储在单独的.java文件中的另一个类的新实例,并且许多类(每个在它们自己的.java文件中)似乎引用其他类而不必放置任何东西,例如

This creates a new instance of another class which is stored in a separate .java file, and many of the classes (each in their own .java file) seem to reference other classes without having to put anything such as

include("otherclass.php")

如果你使用PHP。

我的问题是:这是Java怎么做的?所以你可以愉快地引用其他类,并从另一个.java文件创建一个类的新实例,只要它们在同一个目录中?

My question is: Is this how Java does things? So you can happily refer to other classes and create new instances of a class from another .java file as long as they are in the same directory?

我希望我的问题有些感觉!

I hope my question makes some sense!

谢谢,

杰克。

推荐答案

import 语句用于。其他类只需要在类路径中。类路径基本上是指向包根和/或单个JAR文件的文件系统路径的集合。使用 import 语句确实不需要,当类与当前类(同一目录,如你所说)相同的包,或当你引用它们完全限定名如

There the import statement is for. The other class only has to be in the classpath. The classpath is basically a collection of file system paths pointing to the package root and/or individial JAR file(s). Using the import statement is indeed not necessary when the class is in the same package as the current class (the same directory, as you say), or when you're referencing them by the full qualified name like

com.example.OtherClass otherClass = new com.example.OtherClass();

此外, java.lang 包总是隐式导入,您不需要显式导入它们,也不需要在类路径中显式指定它们的路径。

Also, the classes of the java.lang package is always implicitly imported, you don't need to explicitly import them nor explicitly specify their path in the classpath.

  • Java tutorial - Using package members

这篇关于在Java中访问其他类文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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