(主观)无效的Java类构造函数

huangapple go评论100阅读模式
英文:

(Subjective) Invalid Java Class Constructor

问题

我正在编写一个Java类,其中给定的参数可能无效,但仍然是正确的类型。我希望我的类接受两个整数,但不允许第二个整数为零。我有哪些可能性来中断构造函数,也许手动返回null?我的可能性有哪些?

  1. public class Test {
  2. private int a, b;
  3. public Test(int p1, int p2) {
  4. if (p2 == 0) {
  5. // Do something to handle the invalid parameter, such as throwing an exception.
  6. } else {
  7. a = p1;
  8. b = p2;
  9. }
  10. }
  11. }
英文:

I am programming a java class, where the given parameters could be invalid, but still be the right type. I want my class to take two integers, but the second one is not allowed to be zero. What do I have for possibilities to interrupt the Constructor, maybe returning manually null? What are my possibilities?

  1. public class Test {
  2. private a, b;
  3. public Test(p1, p2) {
  4. if (p2 == 0) return null;
  5. a = p1;
  6. b = p2;
  7. }
  8. }

答案1

得分: 2

  1. 构造函数没有返回值因此返回 `null` 不是一个选项惯用的做法是抛出 [`IllegalArgumentException`](https://docs.oracle.com/javase/8/docs/api/java/lang/IllegalArgumentException.html):
  2. ```java
  3. public class Test {
  4. private a, b;
  5. public Test(p1, p2) {
  6. if (p2 == 0) {
  7. throw new IllegalArgumentException("p2 不能为零");
  8. }
  9. a = p1;
  10. b = p2;
  11. }
  12. }
英文:

A constructor doesn't have a return value, so returning null isn't an option. The idiomatic thing to do would be to throw an IllegalArgumentException:

  1. public class Test {
  2. private a, b;
  3. public Test(p1, p2) {
  4. if (p2 == 0) {
  5. throw new IllegalArgumentException("p2 can't be null");
  6. }
  7. a = p1;
  8. b = p2;
  9. }
  10. }

答案2

得分: 2

如果您想在参数无效的情况下返回 null,不能使用构造函数。

相反,将构造函数设置为私有,并编写一个静态工厂方法:

  1. private Test(int a, int b) {
  2. // 任何操作。
  3. }
  4. public static Test create(int a, int b) {
  5. if (/* a 和 b 无效 */) {
  6. return null;
  7. }
  8. return new Test(a, b);
  9. }

然后,您应该调用 Test.create 而不是 new Test

但前提是您不打算扩展 Test

英文:

If you want to return null in the case of invalid parameters, you can't use a constructor.

Instead, make the constructor private, and write a static factory method:

  1. private Test(int a, int b) {
  2. // Whatever.
  3. }
  4. public static Test create(int a, int b) {
  5. if (/* a and b are invalid */) {
  6. return null;
  7. }
  8. return new Test(a, b);
  9. }

You then invoke Test.create instead of new Test.

This is only an option if you don't want to extend Test, though.

答案3

得分: 0

根据您的需要,有很多可能性,这是最重要的事情。

一个想法可能是,如果第二个参数为0,就抛出IllegalArgumentException异常,这样构造函数会以错误退出,而类不会被创建。

英文:

Quite a lot of possibilities, depending on what you need, which is the most important thing.

An idea could be throw an IllegalArgumentException if the 2nd parameter is 0 so the constructor exits with error and the class is not created.

huangapple
  • 本文由 发表于 2020年8月31日 02:37:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63660899.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定