隐藏带有子类参数的方法

发布于 2024-12-01 10:08:38 字数 1897 浏览 0 评论 0原文

不确定这是否被称为“方法隐藏”或“方法覆盖”,或者两者都不是,并且希望被引导到一些关于该主题的好文章。特别是,它是否是好的做法,何时和何时不使用它,以及使用它的优点/缺点。

public class Animal {

  /* constructor */
  Animal () { }

  /* instance method */
  void add(Number n) {
    System.out.println("An animal added a number!");
  }

  /* Main method */
  public static void main(String[] args) {
    Integer i = 2;   // Integer i = new Integer(2);
    Double d = 3.14; // Double d = new Double(3.14);

    Animal mammal = new Animal();
    Cat tom = new Cat();
    Mouse jerry = new Mouse();

    mammal.add(i); // produces "An animal added a number!"
    mammal.add(d); // produces "An animal added a number!"

    tom.add(i);    // produces "Tom added an integer!"
    tom.add(d);    // produces "An animal added a number!"

    jerry.add(i);  // produces "An animal added a number!"
    jerry.add(d);  // produces "Jerry added a double!"
  }
}

class Cat extends Animal {

  /* constructor */
  Cat () { }

  /* instance method */
  void add(Integer i) {
    // param of type Integer extends Number
    System.out.println("Tom added an integer!");
  }
}

class Mouse extends Animal {

  /* constructor */
  Mouse () { }

  /* instance method */
  void add(Double d) {
    // param of type Double extends Number
    System.out.println("Jerry added a double!");
  }
}

编辑:

感谢@MByD,发现这称为“方法重载”。

与上述相关的新问题: 在 Animal 类中,我想创建一个方法,该方法采用 Number 对象并使用子类中重载的 add() 方法之一老鼠。有没有比下面所示更好的方法来做到这一点?

public class Animal {
...
  void subtract(Number n) {
    if      (n instanceof Integer) this.add(-(Integer) n); // from the Cat class
    else if (n instanceof Double)  this.add(-(Double) n);  // from the Mouse class
    ...
  }
...
}

是的,我意识到我可以只编写 this.add(-n),但我想知道是否有一种方法可以选择依赖于参数子类的实现。由于参数是抽象类型并且无法实例化,因此我必须将子类作为参数传递。

Not sure if this is called "method hiding" or "method overriding", or neither, and would like to be directed to some good articles about the subject. Especially, whether or not it is good practice, when and when not to use it, and the advantages/disadvantages of using it.

public class Animal {

  /* constructor */
  Animal () { }

  /* instance method */
  void add(Number n) {
    System.out.println("An animal added a number!");
  }

  /* Main method */
  public static void main(String[] args) {
    Integer i = 2;   // Integer i = new Integer(2);
    Double d = 3.14; // Double d = new Double(3.14);

    Animal mammal = new Animal();
    Cat tom = new Cat();
    Mouse jerry = new Mouse();

    mammal.add(i); // produces "An animal added a number!"
    mammal.add(d); // produces "An animal added a number!"

    tom.add(i);    // produces "Tom added an integer!"
    tom.add(d);    // produces "An animal added a number!"

    jerry.add(i);  // produces "An animal added a number!"
    jerry.add(d);  // produces "Jerry added a double!"
  }
}

class Cat extends Animal {

  /* constructor */
  Cat () { }

  /* instance method */
  void add(Integer i) {
    // param of type Integer extends Number
    System.out.println("Tom added an integer!");
  }
}

class Mouse extends Animal {

  /* constructor */
  Mouse () { }

  /* instance method */
  void add(Double d) {
    // param of type Double extends Number
    System.out.println("Jerry added a double!");
  }
}

EDIT:

Thanks to @MByD, found out this is called "method overloading".

New question related to above:
In the Animal class, I want to create a method that takes a Number object and uses one of the overloaded add() methods in the subclasses Cat and Mouse. Is there a better way to do this than what's shown below?

public class Animal {
...
  void subtract(Number n) {
    if      (n instanceof Integer) this.add(-(Integer) n); // from the Cat class
    else if (n instanceof Double)  this.add(-(Double) n);  // from the Mouse class
    ...
  }
...
}

Yes, I realize I could just write this.add(-n), but I'm wondering if there's a way to choose an implementation dependent on the subclass of a parameter. Since the parameter is an abstract type and can't be instantiated, I must pass a subclass as an argument.

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

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

发布评论

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

评论(1

人事已非 2024-12-08 10:08:38

这称为方法重载,因为方法的签名不相同。

请参阅方法的 Java 教程

Java编程语言支持重载方法,并且Java
可以区分具有不同方法签名的方法。这
意味着类中的方法可以具有相同的名称,如果它们具有
不同的参数列表(对此有一些限制
将在标题为“接口和继承”的课程中讨论)。

是否以及何时使用各种重载/覆盖/遮蔽等的争论是一个很大的争论。 Joshua Bloch 所著的《Effective Java》一书是一个非常好的资源。 。我发现这非常有用且有趣。

This is called method overloading, since the signature of the methods are not the same.

See the Java Tutorials of methods:

The Java programming language supports overloading methods, and Java
can distinguish between methods with different method signatures. This
means that methods within a class can have the same name if they have
different parameter lists (there are some qualifications to this that
will be discussed in the lesson titled "Interfaces and Inheritance").

The argument whether and when to use all kinds of overloading / overriding / shadowing etc. is a big one. A very good resource is the book Effective Java, By Joshua Bloch. Which I found very useful and interesting.

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