简体   繁体   English

如何在java中为类注入接口?

[英]how to inject interface to class in java?

i have my DTO class that is : 我有我的DTO课程:

public class EmailResponse {

    // Make public to avoid getters and setters
    public Email email;
    public RequestData reqData;    

    public EmailResponse() {
        super();
    }

}

and i want to implement to it this interface: 我想实现这个界面:

public interface IAssertionErrorDo {
    public void onErrorDo();
}

but i want to do it during execution, i don't want to touch "EmailResponse" because it would not be ok to make it implements that interface due they don't belong to the same layer, i mean, EmailResponse would belong to service layer and IAssertionError would belong to test layer. 但我想在执行期间这样做,我不想触摸“EmailResponse”,因为它不可能使它实现该接口,因为它们不属于同一层,我的意思是,EmailResponse属于服务layer和IAssertionError属于测试层。 I am using TestNG. 我正在使用TestNG。

Do you know how i could do this? 你知道我怎么做吗? Regards 问候

EDIT: My implementation is this: 编辑:我的实现是这样的:

EmailResponse emailResponse = emailService.getUserEmail(userId);

And the reason i want to do this "injection" is because i have 我想做这个“注射”的原因是因为我有

public class LoggingAssert
    extends Assertion {

    private static final Logger LOGGER = LoggerFactory.getLogger(LoggingAssert.class);
    private IAssertionErrorDo dataE;
    @Override
    public void onAssertFailure(IAssert a, AssertionError ex) {
        LOGGER.info("[ERROR] " + a.getMessage());
        if (this.dataE != null) {
            this.dataE.onErrorDo();
        }
    }

    public LoggingAssert setOnErrorDo(IAssertionErrorDo object) {
        this.object = object;
        return this;
    }
}

loggingAssert.setOnErrorDo(emailResponse).assertNotNull(emailResponse.getEmail().getId(),
            "Checking created email doesn't exists");

So i want to if assert fails execute method onErrorDo() from emailResponse 所以我想如果断言从emailResponse失败执行方法onErrorDo()

You could do 你可以做到

public class MyEmailResponse extends EmailResponse implements IAssertionErrorDo {
...
}

implementation calls in interfaces, you can call more than 1 interface if you want by adding commas to separate them.. 在接口中实现调用,如果需要,可以通过添加逗号来分隔它们来调用多个接口。

to call interface methods you simply just use the method's name. 要调用接口方法,只需使用方法的名称即可。

like this: 像这样:

public class MyEmailResponse implements IAssertionErrorDo 
{
  public void onErrorDo() {//define it's behavior}
} 

if you extend a class you use: super.MyMethod() to call the a method inside the extended class, but if you already have an extended class and want a method from another class you have to create an object for that class first then call it, thus: 如果你扩展一个你使用的类: super.MyMethod()来调用扩展类中的一个方法,但如果你已经有一个扩展类并且想要一个来自另一个类的方法,你必须首先为该类创建一个对象然后调用因此:

MyClass mc = new MyClass();

if it is in a different package then 如果是在不同的包装中

myPackage.MyClass mc = new myPackage.MyClass(); then you call your method from that class using the object you created, which is in this case mc.. so: 然后你使用你创建的对象从该类调用你的方法,在这种情况下是mc ..所以:

mc.MyMethod();

if you want it to return a variable then you will need to add a return statement in that method with the variable you want it to return. 如果您希望它返回一个变量,那么您需要在该方法中添加一个返回语句,并使用您希望它返回的变量。

interfaces are usually used for global an changing environments (dynamics), for example if you developed a program and it needs a driver to connect to databases then you will make an interface and send it to the database developers, and each one will fill the codes in that interface and send it back... this guarantees consistency. 接口通常用于全局变化的环境(动态),例如,如果您开发了一个程序并且它需要一个驱动程序来连接到数据库,那么您将创建一个接口并将其发送给数据库开发人员,并且每个接口都将填充代码在该界面中并将其发回...这可以保证一致性。 when you implement an interface you have to define every method inside it (even if you leave it empty) and you cannot change the interface's methods names nor add... it is used in other areas as well, i don't think you need to use it in your case. 当你实现一个接口时,你必须定义它里面的每个方法(即使你把它留空)你也不能改变接口的方法名称,也不能添加......它也用在其他方面,我觉得你不需要在你的情况下使用它。

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

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