[英]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:我在这里看到三点:
Dependency Injection (DI) is a different thing, you like to do old-school object-orientated-programming (OOP).依赖注入(DI)是另一回事,你喜欢做老派的面向对象编程(OOP)。
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);
The syntax of the insert-statement is wrong.插入语句的语法是错误的。 Do you need support for the correct syntax?
您需要支持正确的语法吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.