假设我有两个类 A 和 B 。如果我在类 B 中创建A类对象,并在类 A 中创建B类对象,则会导致Stack Overflow错误。这个问题的一个解决方案是我可以在B类的任何函数内创建一个类 A 的对象,反之亦然但如果我这样做,那么创建类 A 的对象每次创建类 A 的对象的特定函数时,都会被调用。
问题是如何有效地将对象 A 和 B 放在彼此内?
请考虑以下示例。
房间等级
public class Room {
String roomno;
String reserved;
String category;
String airconditioned;
String bedtype;
String rent;
Connection con;
PreparedStatement ps;
ResultSet rs;
AddRoom adr = new AddRoom();
RemoveRoom rr = new RemoveRoom();
UpdateRoom ur = new UpdateRoom();
// AllRooms alr = new AllRooms();
public Room()
{
roomno = "";
reserved = "";
category = "";
airconditioned = "";
bedtype = "";
rent = "";
make_connection();
}
public void make_connection()
{
try{
String driver = "net.ucanaccess.jdbc.UcanaccessDriver";
Class.forName(driver);
String login = "jdbc:ucanaccess://C:\\MsDatabase\\EmployeeDB.accdb";
con = DriverManager.getConnection(login);
}catch(Exception ex){ System.out.println(ex);}
}
public void add_room(AddRoom obj)
{
try{
adr = obj;
if("".equals(adr.get_jtextfield1().getText())||"".equals(adr.get_jtextfield2().getText())||
"".equals(adr.get_jtextfield3().getText())||"".equals(adr.get_jtextfield4().getText())||
"".equals(adr.get_jtextfield5().getText())||"".equals(adr.get_jtextfield6().getText()))
{
JOptionPane.showMessageDialog(null, "None of the fields can be left empty");
}
else
{
roomno = adr.get_jtextfield1().getText();
reserved = adr.get_jtextfield2().getText();
category = adr.get_jtextfield3().getText();
airconditioned = adr.get_jtextfield4().getText();
bedtype = adr.get_jtextfield5().getText();
rent = adr.get_jtextfield6().getText();
String sql = "INSERT INTO RoomInfo(RoomNumber,Reserved,RoomCategory,AirConditioned,BedType,RentPerDay)"
+ "VALUES(?,?,?,?,?,?)";
ps = con.prepareStatement(sql);
ps.setInt(1, new Integer(roomno));
ps.setString(2, reserved);
ps.setString(3, category);
ps.setString(4, airconditioned);
ps.setString(5, bedtype);
ps.setInt(6, new Integer(rent));
ps.executeUpdate();
JOptionPane.showMessageDialog(null, "Room Added Successfully");
}
}catch(Exception ex){
JOptionPane.showMessageDialog(null, "Input in Room Number and "
+ "Rent Per Day should be a number");
}
}
}
AddRoom Class
public class AddRoom extends javax.swing.JFrame {
Room objr = new Room();
public AddRoom() {
initComponents();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//Room objr = new Room();
objr.add_room(this);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
AdminHome admh = new AdminHome();
admh.setVisible(true);
dispose();
}
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new AddRoom().setVisible(true);
}
});
}
public JTextField get_jtextfield1()
{
return jTextField1;
}
public JTextField get_jtextfield2()
{
return jTextField2;
}
public JTextField get_jtextfield3()
{
return jTextField3;
}
public JTextField get_jtextfield4()
{
return jTextField4;
}
public JTextField get_jtextfield5()
{
return jTextField5;
}
public JTextField get_jtextfield6()
{
return jTextField6;
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField4;
private javax.swing.JTextField jTextField5;
private javax.swing.JTextField jTextField6;
// End of variables declaration
}
请注意,我在AddRoom类中创建了Room类的对象,并在Room类中创建了AddRoom类的对象。现在,如果我这样做,那么我得到堆栈溢出错误,但如果我在AddRoom类中的任何函数内部使用Room类的对象,则不会显示堆栈溢出错误,程序运行正常。
答案 0 :(得分:1)
您正在使用Circle参考/依赖项,这不是一个好主意......
你需要实现的是一个回调,所以A可以调用B和B中的方法可以通知A看到上面的图像:
//Interface
public interface ICallback{
void onMessage(String msg);
}
//the class A call methods from B
public class A implements ICallback{
private B b;
public A(){
b= new B();
b.setCallback(this);
b.printSomething(5);
b.printSomething(0);
}
@Override
public void onMessage(String msg){
}
}
//the Class B can communicate to A with the callback
public class B {
private ICallback cb;
public B(){
}
public void setCallback(ICallback cb){
this.cb = cb;
}
public void printSomething(int i){
if(i==0){
cb.onMessage("this is zero");
}
}
}
答案 1 :(得分:1)
如何有效地将A类和B类对象放在彼此内部 ?
不建议在Java(或任何OOP语言)中执行此操作,因为它会创建循环依赖项。
如果处理不当,会导致StackOverflowError
之类的异常,或者在程序/应用程序运行时,在解析依赖关系以创建对象时会出现异常(IOC容器会像Spring一样抛出异常,如果你使用的话)。
因此,即使您在没有上述问题的情况下进行管理,在代码中的Java类或包之间创建循环依赖关系也不是一个好习惯,因为它很难理解/维护和代码复杂性很高。这就是像FindBugs这样的代码质量工具有助于识别循环依赖关系的原因,这样我们就可以在开发过程中避免它们。
您可以查看here了解有关同一主题的更多详情。
答案 2 :(得分:0)
不是100%肯定你的意思,你有这样的课吗?...
public class A {
private B b;
public void setB(B b) {
this.b = b;
}
}
public class B {
private A a;
public void setA(A a) {
this.a = a;
}
}
你想创建一个带有“B”的“A”吗?例如......
public class Factory {
public static A createA() {
A a = new A();
B b = new B();
a.setB(b);
b.setA(a);
return a;
}
}
这有帮助吗?