Java 类实例化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13019515/
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
Java Class Instantiation
提问by Michael Harness
I have a file, main.java, and several other files in the same package. Each file is something.class, and I would like to execute them, but I'm having problems locating how to instantiate the class from a something.java file, and then execute it. I am new to java, this is only the second program I've written so please be gentle.
我在同一个包中有一个文件 main.java 和其他几个文件。每个文件都是 something.class,我想执行它们,但是我在定位如何从 something.java 文件实例化类然后执行它时遇到问题。我是 Java 新手,这只是我写的第二个程序,所以请保持温和。
One of the files I'm using is startmessage.java, and here is what I've tried:
我正在使用的文件之一是 startmessage.java,这是我尝试过的:
Object StartMessage = new StartMessage();
Object StartMessage = new StartMessage();
I don't even know if that's correct. Any and all help is very much appreciated.
我什至不知道这是否正确。非常感谢任何和所有帮助。
回答by KaeruCT
The class files are the compiled classes. Source code for Java classes usually go in a file named after themselves. For example, for your startmessage class, you would use StartMessage.java.
类文件是编译后的类。Java 类的源代码通常放在以它们自己命名的文件中。例如,对于 startmessage 类,您将使用 StartMessage.java。
From another file, you can import the class, and then you can instantiate it an object of that type.
从另一个文件中,您可以导入该类,然后您可以将其实例化为该类型的对象。
For example:
例如:
package example;
import StartMessage;
public class Example {
public static void main (String args[]) {
StartMessage startMessage = new StartMessage();
}
}
回答by Arouri
To execute a precompiled java class (.class) it should have a main method to be executed and it can be run using command java http://www2.cs.uic.edu/~sloan/CLASSES/java/
For the second issue, please take a look at the following tutorial: http://docs.oracle.com/javase/tutorial/java/javaOO/index.html
要执行预编译的 java 类 (.class),它应该有一个要执行的 main 方法,并且可以使用命令 java http://www2.cs.uic.edu/~sloan/CLASSES/java/运行它
第二个问题请看下面的教程:http: //docs.oracle.com/javase/tutorial/java/javaOO/index.html
回答by Paulius Matulionis
Well, first thing your file name must be exactly as a class name. For e.g.: If you named the file startmessage.java
then you class should look like this:
好吧,首先您的文件名必须与类名完全相同。例如:如果你命名了文件,startmessage.java
那么你的类应该是这样的:
public class startmessage { }
But it is not the way to name java classes. The proper way is:
但这不是命名 java 类的方式。正确的做法是:
public class StartMessage {}
To instantiate a class you should do like this:
要实例化一个类,你应该这样做:
StartMessage message = new StartMessage();
If you do like this:
如果你喜欢这样:
Object message = new StartMessage();
You won't be able to access methods of StartMessage
class. The only methods will be available from the Object
class.
您将无法访问StartMessage
类的方法。唯一的方法将在Object
类中可用。
回答by user1697575
You can instantiate your class by creating an instance (construct it). For example:
您可以通过创建一个实例(构造它)来实例化您的类。例如:
public class MyClass
{
public void myMethod1() {System.out.println("Hello from method 1");}
public void myMethod2() {System.out.println("Hello from method 2");}
}
public class Runner
{
public static void main(String[] args)
{
// declare variable of type MyClass
MyClass myInstance = new MyClass();
// now execute its methods
myInstance.myMethod1();
myInstance.myMethod2();
}
}
This will produce output in the console:
这将在控制台中产生输出:
Hello from method 1
Hello from method 2
As per your example in the question, you should use StartMessage instead of Object, e.g.
根据您在问题中的示例,您应该使用 StartMessage 而不是 Object,例如
StartMessage myVariable = new StartMessage();
回答by case1352
Case is important.
案例很重要。
If your file really is called startmessage.java, and you've compiled it, and you want to create and instance of it, then try:
如果您的文件确实被称为 startmessage.java,并且您已经编译了它,并且您想要创建它的实例,那么请尝试:
startmessage sm = new startmessage();
回答by Mr_Spock
You have to import the Java class you'd like to utilize throughout your code:
您必须导入要在整个代码中使用的 Java 类:
package <package_name>
import <location_of_class>
Like so:
像这样:
package michael;
import parent.child.*; //use any method with *
Useful link on imports and packages: http://www.leepoint.net/notes-java/language/10basics/import.html
有关导入和包的有用链接:http: //www.leepoint.net/notes-java/language/10basics/import.html
回答by dunadar
In Java, files must be named exactly after the class they contain. In most cases this is a case-sensitive rule. Check it out.
在 Java 中,文件必须完全以其包含的类命名。在大多数情况下,这是区分大小写的规则。看看这个。
回答by FThompson
That code segment would compile (assuming you have a StartMessage class with a default constructor), but it's not necessarily correct. Generally, you will want to declare objects in the following fashion:
该代码段可以编译(假设您有一个带有默认构造函数的 StartMessage 类),但它不一定正确。通常,您需要以下列方式声明对象:
<type> <name> = new <type>(<args>);
By this design, your code segment would be more correct in the following form:
通过这种设计,您的代码段在以下形式中会更正确:
StartMessage message = new StartMessage();
However, because StartMessage is a subclass of Object, and variables can be named nearly anything, your original code compiles fine.
但是,由于 StartMessage 是 Object 的子类,并且变量几乎可以命名为任何名称,因此您的原始代码可以很好地编译。