java 你如何向主方法添加类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3704139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you add classes to a main method
提问by doug
I have three .java files and I need to get them to work together. I think that I need to add all the classes to a main method but I am not sure if this is correct and if I just add the name of the class and the format.
我有三个 .java 文件,我需要让它们一起工作。我认为我需要将所有类添加到主方法中,但我不确定这是否正确,是否只添加类的名称和格式。
I figured it out, the three files had a package listed at the top of each. I created a new Java project in Eclipse and then a source folder and in the source folder I created a package with the name that they all referenced. Now it runs. Thanks for all of you help for the Eclipse/Java beginner.
我想通了,这三个文件在每个文件的顶部都有一个包。我在 Eclipse 中创建了一个新的 Java 项目,然后创建了一个源文件夹,在源文件夹中我创建了一个包,其名称均被引用。现在它运行了。感谢大家对 Eclipse/Java 初学者的帮助。
回答by pstanton
If they are in the same package you do not need to do anything, as they are automatically imported for you, but otherwise you'll need to add import
statements before your class declaration.
如果它们在同一个包中,您不需要做任何事情,因为它们会自动为您导入,否则您需要import
在类声明之前添加语句。
Once this is done, you can reference static members directly ie ClassB.staticMethod();
or instantiate the class ie ClassB classb = new ClassB();
完成后,您可以直接引用静态成员 ieClassB.staticMethod();
或实例化类 ieClassB classb = new ClassB();
But honestly, if you are this confused, you need to spend some more time doing tuturials.
但老实说,如果您对此感到困惑,则需要花更多时间进行教程。
回答by jrharshath
You are right: what you think is not right :P
你是对的:你认为不对的:P
Java can find the classes that you need, you can just use them straight away. I get the feeling that you come from a C/C++ background (like me) and hence think that you will need to "include" the other classes.
Java 可以找到您需要的类,您可以直接使用它们。我觉得你来自 C/C++ 背景(像我一样),因此认为你需要“包含”其他类。
java uses the concept of namespaces and classpaths to find classes. Google around for it.
java 使用命名空间和类路径的概念来查找类。谷歌一下。
A little example of how variety of classes can be used together:
关于如何一起使用各种类的一个小例子:
// A.java
public class A {
public void sayIt() { sysout("Said it by A!"); }
}
// B.java
public class B {
public void doIt() { sysout("Done it by B!"); }
}
// MainClass.java
public class MainClass {
public static void main(String[] args) {
A aObj = new A();
B bObj = new B();
aObj.sayIt();
bObj.doIt();
}
}
Note that there are no includes/imports here because all of the classes are in the same namespace. If they were not, then you'd need to importthem. I will not add a contrived example for that coz its too much to type, but should google for it. Info should be easy enough to find.
请注意,这里没有包含/导入,因为所有类都在同一个命名空间中。如果不是,则您需要导入它们。我不会为那个添加一个人为的例子,因为它的输入太多,但应该谷歌搜索。信息应该很容易找到。
Cheers,
jrh
干杯,
jrh
回答by Jim Brissom
I am not sure what you mean by "adding classes to a main method". If you want to make use of several classes inside your Java program, just import the needed classes/packages at the beginning and create an instance of each class as you go along.
我不确定“将类添加到主要方法”是什么意思。如果您想在 Java 程序中使用多个类,只需在开始时导入所需的类/包,然后在进行过程中为每个类创建一个实例。
回答by DSdavidDS
I learned this from a beginner program called Jeroo
我从一个名为 Jeroo 的初学者程序中学到了这一点
Basically if I want to create a new "Jeroo", I would write the following on my Main method:
基本上,如果我想创建一个新的“Jeroo”,我会在我的 Main 方法上写下以下内容:
Jeroo Bob = new Jeroo();
{ methods... }
So basically:
所以基本上:
[class] [customnameofclass] = new [class]