作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个类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/
我是一名优秀的程序员,十分优秀!