我可以在 Java 源文件中有宏吗

2022-01-11 00:00:00 macros java

在我的程序中,我多次从控制台读取整数.每次,我都需要输入这一行.

In my program I'm reading integers form console many times. Every time, I need to type this line.

new Scanner(System.in).nextInt(); 

我习惯了 C/C++,我想知道我是否可以定义类似的东西

I'm used to C/C++ and I'm wondering if I can define something like

#define READINT Scanner(System.in).nextInt(); 

然后,在我的 java 程序的每个地方,我都可以将表单控制台读取为

and then, in every place in my java program I can read form console as

int a = new READINT;

但是我阅读了表格书籍 Java 不支持宏.

But I read form books Java does not support macros.

请有人解释一下为什么会这样,我可以用其他方式吗?

Someone please explain me why is it so, and can I do this in any other way.

推荐答案

你可以但你不应该.

您不应该因为以这种方式使用预处理器 被认为是不好的做法,并且有更好、更符合 Java 习惯的方法来解决这个用例.

You shouldn't because using the pre-processor in that way is considered bad practice to start with, and there are better and more Java-idiomatic ways to solve this use case.


Java 本身不支持宏.另一方面,您可以通过 C 预处理器 管道源代码a>(简称CPP)就像C/C++编译链一样.

Java itself doesn't support macros. On the other hand, you could pipe the source code through the C pre processor (CPP for short) just like the C/C++ compile chain does.

这是一个演示:

src/Test.java:

#define READINT (new java.util.Scanner(System.in).nextInt())

class Test {
    public static void main(String[] args) {
        int i = READINT;
    }
}

cpp 命令:

cpp command:

$ cpp -P src/Test.java preprocessed/Test.java

结果:

class Test {
    public static void main(String[] args) {
        int i = (new java.util.Scanner(System.in).nextInt());
    }
}

编译:

$ javac preprocessed/Test.java


您可以使用静态方法编写自己的实用程序类:

You can write your own utility class with a static method instead:

import java.util.Scanner;
class StdinUtil {
    public final static Scanner STDIN = new Scanner(System.in);
    public static int readInt() {
        return STDIN.nextInt();
    }
}

而当你想使用它的时候,你可以静态导入readInt方法:

And when you want to use it, you can statically import the readInt method:

import static StdinUtil.readInt; 

class Test {
    public static void main(String[] args) {
        int i = readInt();
    }
}

(或执行 static import StdinUtil.STDIN; 并使用 STDIN.nextInt().)

(or do static import StdinUtil.STDIN; and use STDIN.nextInt().)


我自己曾经对 Java 代码使用过 CPP 预处理方法!我正在为一门课程创建一个编程作业.我希望能够轻松地从参考解决方案中提取代码骨架.所以我只是使用了几个 #ifdef 来过滤掉解决方案的秘密"部分.这样我就可以维护参考解决方案,并轻松地重新生成代码骨架.

I myself used the CPP preprocessing approach on Java code once! I was creating a programming assignment for a course. I wanted to be able to easily extract a code skeleton out of the reference solution. So I just used a few #ifdefs to filter out the "secret" parts of the solution. That way I could maintain the reference solution, and easily regenerate the code skeleton.

这篇文章已被重写为一篇文章这里.

This post has been rewritten as an article here.

(*) 因为我讨厌用你不应该"来回答问题.此外,一些未来的读者可能有充分的理由希望将 cpp 与 Java 源代码结合使用!

(*) Since I hate answering questions with "you shouldn't". Besides, some future reader may have good reasons for wanting to use the cpp in conjunction with Java sources!

相关文章