简体   繁体   English

Builder中的覆盖方法

[英]Override Method in Builder

Example class: 示例类:

public class Example {

    public boolean exampleMethod() {
        return false;
    }

}

So, now I wanted to add a Builder class (of course I have more things in my Example class). 因此,现在我想添加一个Builder类(当然,在Example类中还有更多东西)。 In there, I'd like to override the exampleMethod() method. 在这里,我想重写exampleMethod()方法。

So I would like to do something like this: 所以我想做这样的事情:

ExampleBuilder builder = new ExampleBuilder();
//override method

Builder: 生成器:

public class ExampleBuilder {

    //somehow override the exampleMethod with
    //given arguments (I could do it with
    //ExampleBuilder extends Example
    //but I don't think that is right)

}

Thanks for your answers in advance! 预先感谢您的回答!

You can Override a declaration of method from interface, so instead you can use : 您可以覆盖接口中的方法声明,因此可以使用:

interface Example {
    public boolean exampleMethod();
}

class ExampleBuilder implements Example{

    @Override
    public boolean exampleMethod() {
      ///
    }
}

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

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