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

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

问题描述

来自C ++环境我习惯将我需要的许多功能分成 funcs.h 文件,然后执行#包括funcs.h,然后将函数原型添加到主.cpp文件中。现在我开始使用java(主要是使用我的Minecraft ModloeaderMp)并且我已经创建了一个 funcs.java 文件,其中有一些预制函数(例如一些文件复制函数,给堆栈的物品等)。我已经在使用语句公共类mod_mine扩展BaseModMp ,有没有办法可以导入函数,或者我可以再做一个 Public class mod_mine扩展funcs

解决方案

你没有 #include 在Java中,你 import package.Class 。从Java 6开始(或者它是5?),您还可以导入静态package.Class.staticMethodOfClass ,这将实现您尝试做的某些形式。 / p>

此外,正如@duffymo所指出的那样, import 只会使您免于系统地为导入的类名添加包名称前缀,或带有包和类名的导入静态方法名称。实际的 #include 语义在Java中根本不存在。



这就是说,有一个funcs .java文件在我看来就像你开始将你的脚趾浸入一些反模式......你应该远离这些。


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?

解决方案

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.

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

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