gpt4 book ai didi

java - 如何从另一个类(class)运行一个类(class)

转载 作者:行者123 更新时间:2023-12-01 18:16:29 24 4
gpt4 key购买 nike

我有一个类erdbuilder和另一个类SQL。我的 erdbuilder 类允许我绘制形状并将它们存储在数组列表 Connection 中。然后,我从 SQL 类访问 arraylist Connection,我将从 arraylist 中检索数据。我在 erdbuilder 类中有一个主类

我想从 erdbuilder 类运行 SQL

我可以用它来访问我的 SQL 类,但不确定这是否是正确的方法。

这是我从 erdbuilder 类调用 SQL 类的代码的一部分

if ((rect != null) && (ell != null)) {
con.add(new Connection(rect,ell));
System.out.println("Size of ArrayList <Connection> is:" + con.size());
SQL sql = new SQL();
sql.display();
}

这是我的 SQL 类。

package project;
import java.awt.Shape;
import java.util.ArrayList;
import project.ERDBUILDER.DrawingBoard.Attribute;
import project.ERDBUILDER.DrawingBoard.Connection;
import project.ERDBUILDER.DrawingBoard.NamedShape;


public class SQL {

public void display() {
ArrayList<Connection> con = new ArrayList<>();

for (int a = 0; a < con.size(); a++) {
NamedShape f = con.get(a).getNamedShape1();
Attribute g = con.get(a).getNamedShape2();
String i = f.getName();
String j = g.getName();

Shape y = f.getShape();
Shape y1 = g.getShape();
System.out.println(i + " AND " + j + " are linked");

}



}
}

实际上,当我运行erdbuilder类时,它并没有调用sql类。怎么了?谁能帮我解决这个问题吗?

最佳答案

display() 中,您正在迭代一个空列表。更改该方法以接受 List 类型的参数,然后在调用它时传递 con (您的 ArrayList)。

以下是按描述编辑的片段:

if ((rect != null) && (ell != null)) {
con.add(new Connection(rect,ell));
System.out.println("Size of ArrayList <Connection> is:" + con.size());
SQL sql = new SQL();
sql.display(con);
}

...

package project;
import java.awt.Shape;
import java.util.List;
import project.ERDBUILDER.DrawingBoard.Attribute;
import project.ERDBUILDER.DrawingBoard.Connection;
import project.ERDBUILDER.DrawingBoard.NamedShape;


public class SQL {

public void display(List<Connection> con) {

for (int a = 0; a < con.size(); a++) {
NamedShape f = con.get(a).getNamedShape1();
Attribute g = con.get(a).getNamedShape2();
String i = f.getName();
String j = g.getName();

Shape y = f.getShape();
Shape y1 = g.getShape();
System.out.println(i + " AND " + j + " are linked");

}



}
}

关于java - 如何从另一个类(class)运行一个类(class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29218680/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com