简体   繁体   中英

An interface with different method parameters

I want to declare an interface be use with several classes
this classes have method with different parameters

interface:

public interface Operation {

public int Add();

}

class A:

public class CLASSA implement Operation{

     public int Add(int id,String name);

}

class B:

public class CLASSB implement Operation{

     public int Add(String name);

}

how to impelement of this interface?

you could make an operand-object

public interface Operation {

public int Add(Operand o);

}

or

public interface Operation {

 public int Add(Operand... o);

}

A few points worth mentioning regarding above answers:

My suggestion would be to implement something like the Command pattern. To decouple the command implementation from its implementations is precisely its intent.

in this case you are not using the same interface, but you can create an hereditary system in which some parameters are automatically inserted:

public class CLASSA{
     public int Add(int id,String name){
         //something
     }
}

public class CLASSB extends CLASSA{
     public int Add(String name){
         super.Add(this.FIXED_ID, name);
         //something
     }
}

public class OPERATION extends CLASSA{
     public int Add(){
         super.Add(this.FIXED_ID, this.FIXED_NAME);
         //something
     }
}

Two solutions
Either define two methods in the interface:

public interface Operation {

public int addName(String name);
public int addIdName(int id, String name);

}

or use a generic paramter

public interface Operation {

    public int add(HashMap<String,String> keyValueParams);
}

But for Operations this should work different.

A better approach would be to declare the parameter as a field in the class. Now pass it as a parameter to the constructor, so now it is specified when a new instance of the class is created.

You cannot do like this. If the interface Operations is yours (not predefined or somebody else), have overloaded abstract methods in it. This is the only way.

Best to try it out:

public interface Operation {
    public int add();
}


public class SimpleOperation implements Operation {

    @Override
    public int add() {
        return 0;
    }

    public int add(int id, String name) {
        return id;
    }

}

When in use:

Operation op = new SimpleOperation();
op.add(); // Valid
op.add(2, "two"); // Invalid

Basically, although you're able to add that method into the class, if it doesn't exist in the interface then you can't call it if the instance you're calling it on is of that interface.

This is valid however, since the instance is of class SimpleOperation , which has the overloaded method declared:

SimpleOperation op = new SimpleOperation();
op.add(); // Valid
op.add(2, "two"); // Also valid

This reduces the usefulness of an interface however. If you're keen on overloading the methods, consider using an abstract superclass with both the methods defined and override them in your subclass. Or, add both the original and overloaded method into the interface.

This is a bad idea, it violates OO design and the idea behind an interface. An interface is supposed to be used as a way to abstract away the method calls from the implementation.

My recommendation is to use the method with both parameters in the interface and just don't use the ID argument in CLASSB or to not have an add method in the interface and just have different versions in each class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM