繁体   English   中英

使用 Helper 类简化复制粘贴过程

[英]Simplify copy-paste process with a Helper class

我正在做一个项目,我有一个问题。 我在 50% 的课程中​​使用此代码。

public sendMessage disableNotification(boolean disableNotification) {
    this.disableNotification = disableNotification;

    return this;
}

是否有一种带有假设类Helper的继承方法,允许我只编写一次该方法?

每个类中唯一改变的是对象。 例如,这是sendMessage其他人可以是sendAudio 我可以简化这个复制粘贴过程吗?

我猜你正在寻找抽象类。

abstract class  Helper {
    private boolean disableNotification;

    public Helper disableNotification(boolean disableNotification) {
        this.disableNotification = disableNotification;
        return this;
    }
}

class sendMessage extends Helper {

}

class sendAudio extends Helper {

}

class Test {
    public static void Test() {
        Helper sendMessage = new sendMessage();
        Helper helper = sendMessage.disableNotification(true);
    }
}
abstract class Helper {
  private boolean disableNotification;

  public Helper disableNotification(boolean disableNotification) {
    this.disableNotification = disableNotification;
    return this;
  }
}

class sendMessage extends Helper {
}

class Test {
  public static void Test() {
    Helper sendMessage = new sendMessage();
    Helper helper = sendMessage.disableNotification(true);
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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