问题
Hi I am facing a design problem which I think it should be quite common:
public abstract class Parent
{
...
public boolean itsOk()
{
return true;
}
public void execute()
{
if (itsOk()){
System.out.println("done");
}
}
}
I need to be able to override itsOK() function in any subclass inherited from 'Parent' even if arguments are different.
public class Example extends Parent
{
public boolean itsOK(int a)
{
if (a==1) return true;
else return false;
}
}
Then when I call execute, I want the subclass' itsOk() method to be invoked.
public static void main(String[] args) {
Example e=new Example();
e.execute();
}
This works ok if the subclass' itsOk() method has no arguments (like the 'Parent's method), so it's an overriding case, but how can I make it when arguments are different?
回答1:
In such a case I would rather try to have the same method signature in the parent and the child class, ie. a real overwriting and not an overloading. Then, your parameter a could be a member of the class Example which would avoid the need for a parameter. Of course it strongly depends on the rest of the code.
回答2:
Call super.itsOk();
in your subclass' itsOk
method.
That is, I'm assuming what you mean is you want to have an overload of itsOk
defined in your subclass which does something new but also invokes the parent class' default implementation of itsOk
.
As an aside, note the terminology: you're not overriding: to do that, the itsOk
in your subclass must have the same method signature as in the parent class. Instead you're overloading creating a brand new method that just happens to have the same name.
回答3:
You can use generics:
public abstract class Parent
{
...
public <T> boolean itsOk(T t)
{
return true;
}
public void execute()
{
if (itsOk()){
System.out.println("done");
}
}
}
public class Example extends Parent<Integer>
{
public boolean itsOK(Integer a)
{
if (a==1) return true;
else return false;
}
}
回答4:
The itsOk(int a)
method in class Example
is not overriding the itsOk()
method in class Parent
- it is an entirely separate method that doesn't have anything to do with the method in class Parent
.
With what value of a
do you want itsOk(int a)
in Example
to be called when you call itsOk()
in Parent
?
You could ofcourse add an itsOk(int a)
method to class Parent
; then the version in Example
would be overriding that version, and in the execute()
method you could call it:
public abstract class Parent {
public boolean itsOk() {
return true;
}
public abstract boolean itsOk(int a);
public void execute() {
if (itsOk(0)) {
System.out.println("done");
}
}
}
Without declaring an itsOk(int a)
method in class Parent
, you cannot call that method on a Parent
object (or on an Example
object, if the type of the variable referring to the object is Parent
).
I don't think this is a common design problem.
回答5:
When arguments are different it no longer is a case of Overriding. It is called Overloading which basically means that you have two distinct methods to call.
来源:https://stackoverflow.com/questions/6124197/alternative-to-overriding-with-different-arguments-in-java-which-is-impossible