Java:是否支持宏?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1732242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 17:46:10  来源:igfitidea点击:

Java: Is there support for macros?

javamacrosboilerplate

提问by Legend

I am just curious on how people solve this. I often write the same type of code all the time. For instance:

我只是好奇人们是如何解决这个问题的。我经常一直编写相同类型的代码。例如:

new Thread() {
   //...
   //...
   //...
   //Change this line
   //...
   //...
}.start();

I keep changing the line where it says "Change this line" and then starting a thread. This change can be one line or a few lines. How would I go about compacting this code?

我一直在更改显示“更改此行”的行,然后开始一个线程。此更改可以是一行或几行。我将如何压缩此代码?

回答by gnud

Well, I guess you could run your java files through the C preprocessor...

好吧,我猜你可以通过 C 预处理器运行你的 java 文件......

回答by Jason Gritman

You can use the template patternto create a base class that contains the common code. For example:

您可以使用模板模式创建包含公共代码的基类。例如:

public abstract class ThreadTemplate extends Thread
{

    public void run() {
        //reusable stuff
        doInThread();
        //more resusable stuff
    }

    abstract void doInThread();

}

Then starting a thread with the boilerplate code is as easy as:

然后用样板代码启动一个线程就像这样简单:

new ThreadTemplate{
   void doInThread() {
       // do something
   }
}.start();

Also, a less elegant solution to save yourself some typing is to use the templating feature of your ide. You can find some info on setting them up in Eclipse hereand you can find a list of useful ones at Useful Eclipse Java Code Templates

此外,一个不太优雅的解决方案是使用您的 ide 的模板功能来节省一些输入。您可以在此处找到有关在 Eclipse 中设置它们的一些信息,您可以在Useful Eclipse Java Code Templates 中找到有用的列表

回答by Tom Hawtin - tackline

One technique is to put the code in an anonymous inner class, and pass that to a method that does the rest.

一种技术是将代码放在匿名内部类中,然后将其传递给执行其余操作的方法。

interface SomeInterface {
    void fn();
}

    executeTask(new SomeInterface {
        public void fn() {
            // Change this line.
        }
    });

private void executeTask(final SomeInterface thing) {
     Thread thread = new Thread(new Runnable() { public void run() {
        //...
        //...
        //...
        thing.fn();
        //...
        //...
     }});
     thread.start();
}

Generally it isn't a good idea to extend Threador other classes if it is unnecessary.

通常,Thread如果没有必要,扩展或其他类不是一个好主意。

回答by djna

It's possible to use Java annotations to generate boilerplate code. Writing your own annotation processor is not hard.

可以使用 Java 注释来生成样板代码。编写自己的注释处理器并不难。

回答by broschb

If it is something that you use in many projects, I would set this up in your IDE. For instance I use eclipse and if you go to Window->Preferences->Java->Editor->Templates you can setup your own templates, and then have them auto complete. For instance I always start typing sysout and then press tab, and eclipse has a built in template to replace that with System.out.println();

如果它是您在许多项目中使用的东西,我会在您的 IDE 中进行设置。例如,我使用 eclipse,如果您转到 Window->Preferences->Java->Editor->Templates,您可以设置自己的模板,然后让它们自动完成。例如,我总是开始输入 sysout 然后按 Tab,eclipse 有一个内置模板来替换它 System.out.println();

There are many pre-built templates in eclipse, so you could look at those for examples on syntax, if that is the IDE you use, if not there may be something similiar in other IDE's

eclipse中有很多预建的模板,所以你可以看看那些关于语法的例子,如果那是你使用的IDE,如果不是,其他IDE中可能有类似的东西

This is very useful, and you could create one for any boiler code you find yourself writing a lot.

这非常有用,您可以为您发现自己编写大量代码的任何锅炉代码创建一个。

回答by Powerlord

In the case of a Thread, you can pass any object that implements Runnableto its constructor.

在线程的情况下,您可以将实现的任何对象传递Runnable给其构造函数。

So, the solution is to create your own class:

因此,解决方案是创建自己的类:

public class MyClass implements Runnable {
    void run() {
        // change this line
    }
}

Unfortunately, run isn't static, so you'll have to create an instance of MyClass first:

不幸的是, run 不是静态的,因此您必须首先创建 MyClass 的实例:

new Thread(new MyClass()).start();

You could also add variables to MyClass and a constructor so you can pass arguments to it.

您还可以向 MyClass 和构造函数添加变量,以便向其传递参数。

Edit: If you need more than just the start method, you can also subclass Threaditself.

编辑:如果您需要的不仅仅是 start 方法,您还可以对Thread自身进行子类化。

回答by Outlaw Programmer

Nope, no macros. For this case, the closest you can get is to create new Runnable instance and pass it to either a function of your own creation or an ExecutorService, which will start the task for you.

不,没有宏。对于这种情况,您可以获得的最接近的是创建新的 Runnable 实例并将其传递给您自己创建的函数或 ExecutorService,后者将为您启动任务。

With Java, there's no good way to get rid of this "boilerplate" code, mainly because you can't pass pointers to functions; everything needs to be an object.

对于 Java,没有什么好方法可以摆脱这种“样板”代码,主要是因为您不能将指针传递给函数;一切都必须是一个对象。