重用Java方法的最佳方法 [英] Best ways to reuse Java methods

查看:159
本文介绍了重用Java方法的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Java和OOP,并且已经在 Project Euler for practice(很棒的网站顺便说一句)。

I'm learning Java and OOP, and have been doing the problems at Project Euler for practice (awesome site btw).

我发现自己一遍又一遍地做同样的事情,比如:

I find myself doing many of the same things over and over, like:


  • 检查整数是否为素数/生成素数

  • 生成斐波纳契数列

  • 检查是否为数字是回文

存储和调用这些方法的最佳方法是什么?我应该写一个实用程序类,然后 import 吗?如果是这样,我是否导入 .class 文件或 .java 来源?我正在使用纯文本编辑器和Mac终端。

What is the best way to store and call these methods? Should I write a utility class and then import it? If so, do I import a .class file or the .java source? I'm working from a plain text editor and the Mac terminal.

谢谢!

推荐答案

您可以将方法放入实用程序class,然后 import 该类(不是文件!)。

You can put your methods into a utility class, then import that class (not the file!).

import my.useful.UtilityClass;

...
boolean isPrime = UtilityClass.isPrime(2);

当事情开始变得更复杂,并且你想在多个项目中重复使用你的东西时,你可以把它放进一个罐子里,把那个罐子加到项目中。然后你可以 import 并以与上面相同的方式使用该类。

When things start to get more complicated, and you want to reuse your stuff across multiple projects, you can put it into a jar and add that jar to the projects. Then you can import and use the class the same way as above.

这篇关于重用Java方法的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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