简体   繁体   中英

How to merge two methods with the same implementation but different parameter types

The operations in my two methods are the same, but the input parameter types are different, so how can I optimize these two methods, it seems that they are not so repetitive? Because their operations are the same, but the parameter types are different, what should I do to make this code more elegant?

public class Main {

    public static void main(String[] args) {
        BaseStudent baseStudent = new BaseStudent();
        baseStudent.setName("base");
        NbdStudent nbdStudent = new NbdStudent();
        nbdStudent.setName("nbd");
        updateName(baseStudent);
        updateName(nbdStudent);
    }

    private static void updateName(BaseStudent student) {
        student.setName("update base");
    }

    private static void updateName(NbdStudent student) {
        student.setName("update base");
    }

}

As suggested by @K.Vu , you could create an abstract class Student that will be extended by both BaseStudent and NbdStudent :

public asbtract class Student {
  private String name;

  public void setName(String name) {
  ...

public class BaseStudent extends Student {
...

public class NbdStudent extends Student {
...

public class Main {
  private static void updateName(Student student) {
    student.setName("update base");
    ...

You should make your students class extend from the same (abstract) base class or implement the same interface.

Using classes

Since the two classes are both students you can define a common parent (lets say BaseStudent is the common parent)

/* BaseStudent.java */
// abstract is optional, depends on if you want
// to make the class instantiable or having some
// methods that are not implemented
abstract class BaseStudent {
  public String name;
  public void setName(String name) { this.name = name; }
}

/* NbdStudent.java */
class NbdStudent extends BaseStudent { }


/* main file */
public class Main {

    public static void main(String[] args) {
        BaseStudent baseStudent = new BaseStudent();
        baseStudent.setName("base");
        NbdStudent nbdStudent = new NbdStudent();
        nbdStudent.setName("nbd");
        updateName(baseStudent);
        updateName(nbdStudent);
    }

    private static void updateName(BaseStudent student) {
        student.setName("update base");
    }
}

Using interfaces

In a more broad way you can abstract your classes to have a common behaviour but a different implementation

/* Named.java */
interface Named {
  public void setName(String name);
}

/* BaseStudent.java */
class BaseStudent implements Named {
  public String name;
  public void setName(String name) {
    // different implementation simple example
    if (name == null) { name = "BaseStudent with no name"; }
    this.name = name;
  }
}

/* NbdStudent.java */
class NbdStudent implements Named {
  public String name;
  public void setName(String name) { this.name = name; }
}

/* main file */
public class Main {

    public static void main(String[] args) {
        BaseStudent baseStudent = new BaseStudent();
        baseStudent.setName("base");
        NbdStudent nbdStudent = new NbdStudent();
        nbdStudent.setName("nbd");
        updateName(baseStudent);
        updateName(nbdStudent);
    }

    private static void updateName(Named student) {
        student.setName("update base");
    }
}

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