0

I'm a C++ programmer learning Java, and I'm having a couple of logistical issues. I was able to recreate the standard "Hello, world!", but now I need to implement a class (well, besides the fact the the Hello program is in a class, too...Java is weird) and test it.

I defined a small class called Node with a couple of integer fields and a simple member function (in Node.java), so my silly question is, "How do I create a place to try it out in another file?" (It's remarkable how so many sources seem to assume that you know these little details)

I'm assuming I'll need a Main.java, with a class Main inside it...plus a "public static void Main(.." method. But how do I access this node class, which is in the same directory? Java doesn't seem to have "#include" ...

4

1 回答 1

2

If the Node class and Main class exist within the same package, then you don't need to specify an import for the class. All classes in the same package are automatically included (more or less).

public class Main {
    public static void main(String args[]) {
        Node node = new Node();
    }
}

If the Node or Main are in different packages, then you need to use import.

So, if Node was in awesome.node package, you're code would look more like...

import awesome.node.Node;

public class Main {
    public static void main(String args[]) {
        Node node = new Node();
    }
}

I'd take a closer look at the Packages trail

于 2012-11-13T03:24:43.030 回答