你如何在java中#include文件? [英] How do you #include files in java?

查看:29
本文介绍了你如何在java中#include文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 C++ 环境,我习惯于将许多我需要的函数拆分到 funcs.h 文件中,然后执行 #include "funcs.h" 然后将函数原型添加到主 .cpp 文件中.现在我开始使用 java(主要是 minecraft ModloeaderMp),我已经制作了一个 funcs.java 文件,其中有一些预制函数(例如,一些用于文件复制的函数,提供项目堆栈等.).既然我已经在使用语句 Public class mod_mine extends BaseModMp,有没有办法可以导入函数,或者我可以只做另一个 Public class mod_mine extends funcs?

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?

推荐答案

你在 Java 中不用#include,你import package.Class.从 Java 6(或者它是 5?)开始,您还可以 import static package.Class.staticMethodOfClass,这将实现您想要做的某些形式.

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.

另外,正如@duffymo 所指出的,import 只会使您免于系统地为导入的类名加上包名,或在导入的静态方法名前加上包和类名.Java 中根本不存在实际的 #include 语义.

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.

也就是说,拥有一个funcs.java"文件在我看来就像你开始涉足一些反模式......你应该远离这些.

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.

这篇关于你如何在java中#include文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆