重用 Java 方法的最佳方法
我正在学习 Java 和 OOP,并且一直在 Project Euler 上解决问题练习(顺便说一句,很棒的网站)。
我发现自己一遍又一遍地做许多相同的事情,例如:
- 检查整数是否是素数/生成素数
- 生成斐波那契数列
- 检查数字是否是回文
存储和调用这些方法的最佳方式是什么?我应该编写一个实用程序类然后导入
它吗?如果是这样,我是否导入 .class
文件或 .java
源?我使用纯文本编辑器和 Mac 终端进行工作。
谢谢!
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:
- checking if an integer is prime/generating primes
- generating the Fibonacci series
- checking if a number is a palindrome
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.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以将方法放入实用程序类中,然后
导入
该类(而不是文件!)。当事情开始变得更加复杂,并且您想在多个项目中重用您的东西时,您可以将其放入一个 jar 中并将该 jar 添加到项目中。然后您可以按照与上面相同的方式
导入
并使用该类。You can put your methods into a utility class, then
import
that class (not the file!).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.UtilityClass
的想法很好,但它也给了你练习的机会测试驱动开发。对于新的欧拉问题,请在您将解决该问题的UtilityClass
中创建一个空方法。然后进行一堆使用此新方法的 JUnit 测试,并依赖它是否正确。测试都会失败(或者应该失败,因为您还没有编写解决方案!)现在解决欧拉问题并观察测试通过!如果您想稍后重用代码,单元测试将使您在重构期间保持正确,并为您可能发现的错误提供添加回归案例的位置。
The
UtilityClass
idea is fine, but it also gives you the opportunity to practice TDD. For a new Euler problem, create an empty method in yourUtilityClass
where you'll solve that problem. Then make a bunch of JUnit tests that use this new method and depend on it being correct. The tests will all fail (or they should, because you haven't written the solution yet!)Now solve the Euler problem and watch the tests pass! If you want to reuse the code later, the unit tests will keep you correct during refactoring and will provide a place to add regression cases for bugs you may find.
用您的答案创建一个目录树。我的/数学/*.java。您应该为该类指定包 my.math。为不同的问题集创建不同的类; Primes.java、Fibonacci.java等。如果你已经解决过一次问题,就不要再解决它,除非你的解决方案被破坏了。
将 my/math 上面的目录添加到您的类路径(java -cp 目录)或 jar 目录并将其添加到您的类路径。
像这样的库是成功项目的命脉。创建和使用解决方案库(该语言还没有库)可以解决许多项目问题。尽可能使用可用的库。
Create a directory tree with your answers. my/math/*.java. You should specify the package my.math for the class. Create different classes for different problems sets; Primes.java, Fibonacci.java, etc. If you have already solved the problem once, don't solve it again, unless your solution is broken.
Add the directory above my/math to your classpath (java -cp directory) or jar the directory and add that to your classpath.
Libraries such as this are the lifeblood of a successful project. Creating and using a library of solutions (for which the language does not already have a library) solves a number of project problems. Use available libraries whenever possible.
为什么不创建一个 jar 文件,一个您自己的常用组件库?如果您编译 java 代码,只需将实用程序存档包含在类路径中即可。
Why not create a jar file, a library of your own, often used components? If you compile your java code, just include the utility archive in your classpath.