How do you #include files in java?

旧城冷巷雨未停 提交于 2019-12-17 20:19:42

问题


Coming from a C++ environment I got used to splitting up many of the functions that I needed into an funcs.h file and then do #include "funcs.h" and then adding the functions prototypes into the main .cpp file. Now I am starting to work with java (mainly with minecraft ModloeaderMp) and I already made a funcs.java file where there are some premade functions (e.g some functions for file copying, giving stacks of items etc.). SInce I am already using the statement Public class mod_mine extends BaseModMp, is there a way I can import the functions or do I can I just do another Public class mod_mine extends funcs?


回答1:


You don't #include in Java, you import package.Class. Since Java 6 (or was it 5?), you can also import static package.Class.staticMethodOfClass, which would achieve some forms of what you're trying to do.

Also, as @duffymo noted, import only saves you from systematically prefixing the imported class names with the package name, or the imported static method names with the package and class name. The actual #include semantics doesn't exist in Java - at all.

That said, having a "funcs.java" file seems to me like you are starting to dip your toes into some anti-patterns... And you should stay away from these.




回答2:


There's no #include in Java.

I would not like a design that had a funcs.java that stored all the variables. Objects are state and behavior encapsulated into a single component. You aren't designing in an object-oriented way if you do that.

Good names matter. A class named Stuff that extends Stuff2 had better just be a poor example.

That's not good Java. I wouldn't consider it to be good C++, either.




回答3:


Java is an object oriented programming language and there is a reason for it.

There is no #include in java., although you can import classes from other packages. Making seperate class func.java to store variables might not be a good idea, until or unless all of them are constants. By extending some class, u can reuse the function. but does extending class pass the IS A test, if not that this might be a bad idea.

If moving from C++, going through some good book, eg Head First java might help a lot.




回答4:


Sounds like you're putting all your methods in the same class. You should separate them:

Utility classes

These should contain static methods that do things like get the contents of a file, show a dialog screen, or add two numbers together. They don't really belong in an object class, they don't require instances, and they're used widely throughout the program. See java.lang.Math for a good example of this.

Constant class or configuration files

This can be a Constants class that contains static final members, like PI = 3.1415. You can access them using Constants.PI.

Or, you can use configuration files and load them into Configuration and access the configuration variables with something like config.get("database")

Other

If your code doesn't fit into any of these, you will want to put it into some class such that your code fits object-oriented programming concepts. From your question, it sounds like you'll want to read up on this. I would first read Head First Java, then maybe some other books on object-oriented programming in Java. After that, I'd look at some design patterns.




回答5:


There is no #include in java . you can use import statement to make classes and interfaces available in your file.




回答6:


Actually... There is a way to have the same semantics as in C's #include (the keyword was later borrowed by C++ for the sake of looking fancy...). It's just not defined with the same words, but it does exactly what you are looking for.

First, let's see what you do with #include in C++ to understand the question:

  • include #defines,
  • "forward" function definitions (their "body" being defined elsewhere, in a class implementation, if you remember Turbo Pascal, you get my point),
  • define structures,

and that's pretty much it.

For the structure definitions, there is no point, that's old-school C: in C++ you don't define struct {} anymore for ages, you define class structures with properties and accessor methods, it's the same in Java: no typedef struct {} here either.

For this, you have the "interface" declaration, see https://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html

It does exactly what you're looking for:

    public interface MyDefines {
        final CHAR_SPACE : ' ';               // ugly #define
        int detectSpace(FileInputStream fis); // function declaration
        // and so on
    }

Then, to use:

    public class MyClass extends MyAncestor implements MyDefines {
        ...
        // implementation of detectSpace()
        int detectSpace(FileInputStream fis) {
            int ret = 0;
            char Car;
            if((Car = fis.read()) != -1) && (Car == CHAR_SPACE)) ret++;
            ...
        }

Read the link given above, it's full of useful cases.




回答7:


You can run the C preprocessor on a Java file, ensuring you use the -P flag to disable line annotations. A quick Google search confirms that this has been attempted at least twice, and is even used in the popular fastutil library:

  • https://programming.guide/java/macros-in-java-source-files.html
  • https://lyubomyr-shaydariv.github.io/posts/2016-09-06-fun-with-java-and-c-preprocessor/

This works for all directives (#include, #define, #ifdef, and so forth) and is both syntactically and semantically identical to the equivalent statements in C/C++.



来源:https://stackoverflow.com/questions/7737766/how-do-you-include-files-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!