简体   繁体   English

从一个类到另一个类的方法的依赖注入

[英]Dependency injection from one class to method of another class

Suppose we have the following class, which defines three values:假设我们有以下类,它定义了三个值:

public class DataForOperations {
    public static Serialization serialize = new Serialization();

    String user_id = serialize.serialization(new TypeRef<User>() {
    }).getUsername();
    int update_id = serialize.serialization(new TypeRef<Result>() {
    }).getUpdate_id();
    String user_message = serialize.serialization(new TypeRef<Message>() {
    }).getText();
}

Then now, in another class, I'd like to call this three values into a method:然后现在,在另一个类中,我想将这三个值调用到一个方法中:

public void InsertDataToDatabase(String user_id, String user_message, int update_id) throws SQLException {
    final Connection connection = DriverManager.getConnection(url, userlogin, password);
    try (Statement statement = connection.createStatement()) {
        statement.executeUpdate("INSERT INTO telegramapitest " + id + update_id + user_id + user_message, statement.RETURN_GENERATED_KEYS);
    }
}

How can i do this?我怎样才能做到这一点? I've read quite a few articles, but they all address dependencies in the same class.我已经阅读了很多文章,但它们都解决了同一个类中的依赖关系。

Welcome HearthWarrio at stackoverflow!欢迎来到 stackoverflow 的 HearthWarrio!

I see three points here:我在这里看到三点:

  1. Dependency Injection (DI) is a different thing, you like to do old-school object-orientated-programming (OOP).依赖注入(DI)是另一回事,你喜欢做老派的面向对象编程(OOP)。

  2. DataForOperations is a class having non-static fields. DataForOperations 是一个具有非静态字段的类。 So in order to access theese 3 values you need to create an instance of the class DataForOperations.因此,为了访问这 3 个值,您需要创建类 DataForOperations 的实例。 You can do this by using您可以使用

    DataForOperations myObject = new DataForOperations(); AnotherClass anotherClassInstance = new AnotherClass(); anotherClassInstance.InsertDataToDatabase(myObject.user_id, myObject.user_message, myObject.update_id);
  3. The syntax of the insert-statement is wrong.插入语句的语法是错误的。 Do you need support for the correct syntax?您需要支持正确的语法吗?

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

相关问题 在另一个班级中使用一个班级的方法? - Using method from one class in another class? 使用从一个类到另一个类的方法 - Using a method from one class into another class 从一个 class 返回到另一个 class 的方法 - Method returning from one class to another class 将依赖关系从一种私有方法传递给另一种私有方法是否是一种有害的依赖关系注入实践? - Is passing a dependency from one private method to another a bad practice of dependency injection? 如何在不破坏依赖注入规则的情况下创建一个包含另一个类的元素列表的类? - How to create a class with a list of elements from another class without breaking the dependency injection rule? 从任何依赖注入容器获取类 - Get class from any Dependency injection container 具有抽象类的依赖注入 - Dependency Injection with an abstract class 插件 Class 的依赖注入 - Dependency Injection for Plugin Class 从接口扩展的单例类的依赖注入 - Dependency Injection of Singleton Class that extends from Interface 如何解决 Null 指针异常,同时调用驱动程序实例从一个 class 到另一个 class 使用依赖注入与 Z1441F197543335CA46300 - How to resolve Null pointer exception while invoke the driver instance fron one class to another class using Dependency injection with cucumber
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM