我有两个使用非常相似的方法,像这样的类:
method classA() {
some variable initialization
get this common interface In
try {
do something
In.methodAA();
catch () {
do something
}
}
method classB() {
some variable initialization
get this common interface In
try {
do something
In.methodBB()
catch () {
do something
}
}
如果可能的话,我想在父类中放置一个方法,因为主要的区别是接口上调用的方法。如何实现呢?
我正在使用java7。
谢谢
您需要提供一种abstract
方法,以使公共接口知道要调用的内容,例如:
abstract class Parent {
public void job() {
try {
doJob();
}
catch (Exception e) {
e.printStackTrace();
}
}
protected abstract void doJob();
}
class Child1 extends Parent {
@Override protected void doJob() {
/* implementation */
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句