Java:支持宏吗?

发布于 2024-08-10 23:39:22 字数 218 浏览 7 评论 0原文

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

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

我不断更改显示“更改此行”的行,然后启动一个线程。这一变化可以是一行或几行。我将如何压缩这段代码?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

毁我热情 2024-08-17 23:39:22

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

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

遮了一弯 2024-08-17 23:39:22

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

public abstract class ThreadTemplate extends Thread
{

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

    abstract void doInThread();

}

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

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

另外,为了节省一些打字时间,一个不太优雅的解决方案是使用 ide 的模板功能。您可以在此处找到有关在 Eclipse 中设置它们的一些信息可以在 有用的 Eclipse Java 代码模板 中找到有用的列表

You can use the template pattern to 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 here and you can find a list of useful ones at Useful Eclipse Java Code Templates

迟月 2024-08-17 23:39:22

一种技术是将代码放入匿名内部类中,并将其传递给完成其余部分的方法。

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();
}

一般来说,如果没有必要,扩展 Thread 或其他类并不是一个好主意。

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 Thread or other classes if it is unnecessary.

倾城泪 2024-08-17 23:39:22

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

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

蒲公英的约定 2024-08-17 23:39:22

如果您在许多项目中使用它,我会在您的 IDE 中进行设置。例如我使用 eclipse 如果你去
窗口->首选项->Java->编辑器->模板您可以设置自己的模板,然后让它们自动完成。例如,我总是开始输入 sysout 然后按 Tab,Eclipse 有一个内置模板可以用 System.out.println() 替换它;

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

这非常有用,你可以为它创建一个您发现自己写了很多的锅炉代码。

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

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

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

揽月 2024-08-17 23:39:22

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

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

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.

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.

岁月如刀 2024-08-17 23:39:22

对于线程,您可以传递任何实现 Runnable 到其构造函数。

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

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

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

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

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

编辑:如果您需要的不仅仅是启动方法,您还可以子类 线程本身。

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

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:

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文