我可以在Java源文件中包含宏吗 [英] Can I have macros in Java source files

查看:79
本文介绍了我可以在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.

推荐答案

可以,但您不应该.

不应,因为以这种方式使用预处理器

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 :

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().)


我本人曾经在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!

这篇关于我可以在Java源文件中包含宏吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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