如何在 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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在 Java 中你不需要
#include
;你导入package.Class
。从 Java 6(或者是 5?)开始,您还可以导入 static package.Class.staticMethodOfClass
,这将实现您想要执行的某些形式。此外,正如 @duffymo 所指出的,
import
只会让您免于系统地为导入的类名称添加包名称前缀,或者为导入的静态方法名称添加包名称和类名称前缀。实际的#include
语义在 Java 中根本不存在。也就是说,在我看来,拥有一个“funcs.java”文件就像你开始涉足一些反模式......你应该远离这些。
You don't
#include
in Java; youimport package.Class
. Since Java 6 (or was it 5?), you can alsoimport 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.
Java 中没有
#include
。我不喜欢具有存储所有变量的 funcs.java 的设计。对象是封装在单个组件中的状态和行为。如果你这样做,你就不是在以面向对象的方式进行设计。
好名字很重要。扩展
Stuff2
的名为Stuff
的类最好只是一个糟糕的示例。这不是一个好的Java。我也不认为它是好的 C++。
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 extendsStuff2
had better just be a poor example.That's not good Java. I wouldn't consider it to be good C++, either.
听起来您将所有方法都放在同一个类中。您应该将它们分开:
实用程序类
它们应该包含静态方法,这些方法可以执行诸如获取文件内容、显示对话框屏幕或将两个数字相加之类的操作。它们并不真正属于对象类,不需要实例,并且在整个程序中广泛使用。请参阅 java.lang.Math 了解这就是一个很好的例子。
常量类或配置文件
这可以是包含
static final
成员的Constants
类,例如PI = 3.1415
。您可以使用Constants.PI 访问它们。或者,您可以使用配置文件并将其加载到
Configuration
中,并使用config.get("database")
之类的方法访问配置变量。其他
如果您的代码不适合其中任何一个,您将需要将其放入某个类中,以便您的代码适合面向对象的编程概念。从您的问题来看,您似乎想阅读此内容。我会首先阅读Head First Java,然后可能是其他一些关于<一href="http://www.google.com/search?gcx=c&sourceid=chrome&ie=UTF-8&q=java%20books#sclient=psy-ab&hl=en&source=hp&q =java%20object%20orient%20programming%20book&pbx=1&oq=java%20object%20orient ed%20programming%20book&aq=f&aqi=g1g-v2&aql=&gs_sm=e&gs_upl=1221l6370l0l6583l37l18l0l14l14l3 l300l2981l1.13.3.1l26l0&bav=on.2,or.r_gc.r_pw.r_cp.,cf.osb&fp=18aa9f7b4fee5b6d&biw=1349&bih=780" rel="nofollow noreferrer">Java 中的面向对象编程。之后,我会查看一些设计模式。
It 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 containsstatic final
members, likePI = 3.1415
. You can access them usingConstants.PI
.Or, you can use configuration files and load them into
Configuration
and access the configuration variables with something likeconfig.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.
Java 是一种面向对象的编程语言,这是有原因的。
Java 中没有#include,尽管您可以从其他包导入类。
创建单独的类 func.java 来存储变量可能不是一个好主意,除非它们都是常量。
通过扩展某个类,您可以重用该函数。但是扩展类是否通过了“is a”测试?如果不是这样,这可能是一个坏主意。
如果从 C++ 转向,请阅读一些好书,例如 Head First Java 可能会有很大帮助。
Java is an object-oriented programming language, and there is a reason for it.
There isn't any #include in Java, although you can import classes from other packages.
Making separate class, func.java, to store variables might not be a good idea, until or unless all of them are constants.
By extending some class, you 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, for example, Head First Java might help a lot.
Java 中没有#include。您可以使用import语句使类和接口在您的文件中可用。
There isn't any #include in Java. You can use the import statement to make classes and interfaces available in your file.
您可以在 Java 文件上运行 C 预处理器,确保使用 -P 标志禁用行注释。快速的 Google 搜索证实,这已经尝试过至少两次,甚至在流行的 fastutil 库中使用:
这适用于所有指令(#include、 #define、#ifdef 等),并且在语法和语义上与 C/C++ 中的等效语句相同。
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:
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++.
实际上...有一种方法可以具有与 C 的#include 相同的语义(该关键字后来被 C++ 借用,以便看起来很花哨...)。它只是没有用相同的词来定义,但它正是您所寻找的。
首先,让我们看看在 C++ 中如何使用 #include 来理解这个问题:
#defines
,仅此而已。
对于结构定义,没有任何意义。这是老式的 C:在 C++ 中,您已经很长时间不再定义
struct {}
了;您可以使用属性和访问器方法定义class
结构。 Java 中也是如此:这里也没有typedef struct {}
。为此,您有“接口”声明(请参阅接口(Java™ 教程 > 学习 Java 语言 > 接口和继承)):
它完全符合您的要求:
然后,使用:
阅读上面给出的链接;它充满了有用的案例。
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:
#defines
,and that's pretty much it.
For the structure definitions, there isn't any point. That's old-school C: in C++ you don't define
struct {}
anymore for ages; you defineclass
structures with properties and accessor methods. It's the same in Java: notypedef struct {}
here either.For this, you have the "interface" declaration (see Interfaces (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)):
It does exactly what you're looking for:
Then, to use:
Read the link given above; it's full of useful cases.