隐藏带有子类参数的方法
不确定这是否被称为“方法隐藏”或“方法覆盖”,或者两者都不是,并且希望被引导到一些关于该主题的好文章。特别是,它是否是好的做法,何时和何时不使用它,以及使用它的优点/缺点。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这称为方法重载,因为方法的签名不相同。
请参阅方法的 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 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.