java - 我对如何在我的代码中实现单例模式并让它调用我的对象类和方法感到困惑

标签 java class design-patterns

正如标题所示,我正在尝试在我的代码中实现单例设计模式。我的代码有一个主类和 3 个继承类。我只想创建一个能够使用代码中所有函数的对象。这是我第一次处理设计模式,所以我的代码可能无处不在。这是代码:

class geniusATM {

    private String name, address;
    private int pin, ficoScore;
    double checkingBalance, savingBalance, mortgageBalance;

    public geniusATM() {

    }

    geniusATM(String nam, String addr, int pn, int fs, double cB, double sB, double mB) {
        name = nam;
        address = addr;
        ficoScore = fs;
        checkingBalance = cB;
        savingBalance = sB;
        mortgageBalance = mB;
    }

         //setters and getters are here
        }

       class Checkings extends geniusATM {
        //stuff here
          }

       class Savings extends geniusATM {
        //stuff here
         }

       class billPay extends Checkings {
       //stuff here

       }

     public class singletonObject {

      private static singletonObject ob;

    private  singletonObject() {
        geniusATM matt = new geniusATM("Matt", "124 Road Drive.", 1234, 3462, 560.00, 500.50, 472.29);

    }

    public static singletonObject getObject() {
        if (ob == null) {
            ob = new singletonObject();
        }
        return ob;
    }

    public static void main(String[] args) {


    }


}

最佳答案

现在使用 ob 变量,您可以获取 4 个类中任何类中创建的所有函数

class geniusATM {

private String name, address;
private int pin, ficoScore;
double checkingBalance, savingBalance, mortgageBalance;

public geniusATM() {

}

geniusATM(String nam, String addr, int pn, int fs, double cB, double sB, double mB) {
    name = nam;
    address = addr;
    ficoScore = fs;
    checkingBalance = cB;
    savingBalance = sB;
    mortgageBalance = mB;
}

     //setters and getters are here
    }

   class Checkings extends geniusATM {
       public Checkings() {

}
     Checkings (String nam, String addr, int pn, int fs, double cB, double sB, double mB) {super( nam, addr,  pn,  fs,cB, sB, mB);}
      }

   class Savings extends Checkings {
       public Savings() {

}
     Savings (String nam, String addr, int pn, int fs, double cB, double sB, double mB) {super( nam, addr,  pn,  fs,cB, sB, mB);}
      }


   class billPay extends Savings {
       public billPay() {

}
   billPay (String nam, String addr, int pn, int fs, double cB, double sB, 
double mB) {super( nam, addr,  pn,  fs,cB, sB, mB);}
      }


 public class singletonObject {

  private static Checkings ob;

private  singletonObject () {
    billPay matt = new billPay ("Matt", "124 Road Drive.", 1234, 3462, 560.00, 500.50, 472.29);

}

public static Checkings getObject() {
    if (ob == null) {
        ob = new billPay();
    }
    return ob;
}

public static void main(String[] args) {


}

}

关于java - 我对如何在我的代码中实现单例模式并让它调用我的对象类和方法感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49930290/

相关文章:

c++ - 如何从 C++ 中自己的定义递归调用类成员函数?

c# - 如何依赖注入(inject)类/类型?

Java - 处理游戏中可切换效果的最佳方法

java - 从 EDT in swing 更新是绝对规则还是有异常(exception)?

java - 在 jTable 中添加数据列

java - 创建 jar 文件的困难

c++ - 构造函数初始列表,这段代码是什么意思?

javascript - JavaScript 类中的属性可以有多个值吗?

c++ - 创建具有受限属性的类型

java - 如何从变量创建自定义类? (不是对象)