Dependent Classes in Java
For our assignment we need to write code for a neural network. The way I planned to do it was to write a Node
class, which is a node within the network; a Layer
class, which is a layer of nodes and a NeuralNet
class, which is a network of layers.
I'm having a lot of trouble understanding the way Java is designed to work for imports. To me it seems that it should be a simple matter to include my Node
class in my Layer
class, and my Layer
class in my NeuralNet
class, however Java doesn't like importing from the default package.
What I have read suggests that anything you import needs to be in a package and packages have their own subdirectory. Because of the way I plan to structure my classes, this leaves me with what s开发者_运维知识库eems to me, an unwieldy and unnecessarily complex directory structure ie.
neuralpkg.layerpkg.nodepkg.Node
Can someone explain to me whether this is the only way to implement the structure that I want or whether there is some much, much simpler way that I've missed?
For what its worth I'd have no trouble writing this in C/C++, but attempting to import in a similar style has only given me heartache.
You don't need to layer the package like neuralpkg.layerpkg.nodepkg
, where "neural", "layer", and "node" are subpackages for the respective classes.
You can simply create a package named "ben" and put all of your classes in there. So within your source directory you'd have a layout like:
--/ben
----Layer.java
----NeuralNetwork.java
----Node.java
Each class definition should then start with the line package ben;
.
Classes in the same package don't need to import each other.
It's more like this:
Make a directory called src. This is the root under which all java files will go. Underneath it, make a series of directories called
com->ben->neural or some such. Put all your java source files in the neural folder.
Also, what IDE are you using? Score yourself Netbeans, Eclipse, or the free version of IntelliJ. Java needs a good IDE. When you create a new class, the IDE will do the package statement for you and help immensely with the imports.
Think of packages in Java like namespaces in C++. From what it sounds, it seems like you want everything in the same namespace. The default package can sort of be thought of as unnamed namespaces (I think).
What you should do is just create a simple package com.class.hw01
for example. And put all your classes in there. That way you don't even have to import anything, you can just declare/use your classes just like in C++ if they were in the same namespace.
精彩评论