无法从另一个类创建类对象

粉红弗洛伊德90

我正在尝试使用 addBranch 方法从 Bank 类创建一个分支对象,但似乎没有创建该对象,因为 Branch 构造函数中的 printOut 打印出类似

“已创建分支空”。使用 Bank 类中的相应方法打印分行列表可以确认这一点,因为列表是空的。

怎么了?

//主要的

    import java.util.Scanner;

public class Main {

    public static Scanner scanner = new Scanner(System.in);
    private static Bank banco = new Bank("Banco Central");

    public static void main(String[] args) {

        printOptions();

        boolean quit = false;
        while (!quit) {
            System.out.println("6. Imprimir lista de opciones");
            int option = scanner.nextInt();
            switch (option) {
                case 0:
                    quit = true;
                    break;
                case 1:
                    addBranch();
                    break;
                case 2:
                    printBranches();
                    break;
                case 6:
                    printOptions();
                    break;
            }
        }
    }

    public static void printOptions() {
        System.out.println("Enter option");
        System.out.println("0. Quit");
        System.out.println("1. Agregar sucursal");
        System.out.println("2. Imprimir sucursales");
        System.out.println("3. Agregar cliente nuevo a sucursal con transaccion inicial");
        System.out.println("4. Agregar transaccion a cliente existente");
        System.out.println("5. Imprimir lista de clientes de sucursal"); //agregar opcional de mostrar transacciones

    }

    public static void addBranch() {
        System.out.println("Ingrese el nombre de la sucursal");
        String name = scanner.nextLine();
        scanner.nextLine();

        banco.addBranch(name);
    }

    public static void printBranches() {
        banco.printBranches();

    }


}

//银行类

import java.util.ArrayList;

public class Bank {

    private String name;
    private ArrayList<Branch> branches = new ArrayList<Branch>();

    //CONSTRUCTOR
    public Bank(String name) {
        this.name = name;
        this.branches = new ArrayList<Branch>();
    }

    public void addBranch(String branchName) {
        Branch newBranch = new Branch(branchName);
        branches.add(newBranch);
        //System.out.println(nuevaSucursal.getName() + "creada");

    }


    //2

    public void printBranches() {
        System.out.println("Lista de sucursales");
        for (int i = 0; i < this.branches.size(); i++) {
            System.out.println(this.branches.get(i).getName());

        }


    }


    //4
    public void addTransaction(Client client, double transaccion) {
        Branch.addTransaction(client, transaccion); //al ser STATIC no hace falta crear un Objeto Sucursal para poder llamarlo

    }


    //GETTERS & SETTERS

    public ArrayList<Branch> getBranches() { //devolver sout mejor?
        return branches;
    }

    public void addSucursal(String sucursal) {
        this.branches.add(new Branch(sucursal));
    }
}

//分支类

import java.util.ArrayList;

public class Branch {

    private String branchName;
    ArrayList<Client> clients;

    public Branch(String name) {
        this.branchName = name;
        this.clients = new ArrayList<Client>();
        System.out.println("Sucursal " + name + " creada.");
    }

    public void agregarCliente(String name, double initialTransaction) {
        clients.add(new Client(name, initialTransaction));
        System.out.println("Cliente " + name + " agregado a esta sucursal. Transaccion inicial: " + initialTransaction);
    }

    public static void addTransaction(Client client, double transaccion) {
        client.addTransactions(transaccion); //al ser STATIC no hace falta crear un Objeto Sucursal para poder llamarlo

    }

    public static Branch agregarSucursal(String nombre) {
        return new Branch(nombre);
    }

//GETTERS SETTERS

    public String getName() {
        return this.branchName;
    }
}

//客户

    import java.util.ArrayList;

public class Client {

    private String clientName;
    ArrayList<Double> transactions = new ArrayList<Double>();

    public Client(String clientName, double initialTransactions) {
        this.clientName = clientName;
        this.transactions.add(initialTransactions);
        System.out.println("Cliente creado");
    }

    public String getClientName() {
        return clientName;
    }

    public void setClientName(String clientName) {
        this.clientName = clientName;
    }

    public ArrayList<Double> getTransactions() {
        return transactions;
    }

    public void addTransactions(double transaction) {
        this.transactions.add(transaction);
    }
}
充满鳗鱼的气垫船

你错了——你用这个创建对象就好了:

Branch newBranch= new Branch(branchName);

所以对象创建不是问题。相反,问题在于您在创建对象后什么都不做——您必须在创建后将其放入 ArrayList 以便程序的其余部分可以访问它:

public void addBranch(String branchName) {
   Branch newBranch= new Branch(branchName);
   branches.add(newBranch);
}

这相当于在商店购买杂货但忘记带回家的编程。不要这样做,而要记住将您的物品带回家。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

用另一个类创建新对象

访问在另一个类中创建的对象

访问在另一个类中创建的对象

创建另一个类python的对象

无法从另一个类获取SQL对象

尝试创建一个包含另一个类中的对象的类

基于Python中的另一个对象属性创建一个类的对象

在另一个类中创建一个类的对象,在UML类图中是什么关系?

你如何创建一个使用另一个类的变量的java对象?

如何创建用Java的另一个类封装的非静态类的对象?

如何创建在模块内部并继承了另一个类的类的对象?

如何通过创建另一个类的对象的类访问方法?

我不能从另一个类创建这个类的对象

如何链接到类类型创建另一个类类型的对象

它在创建另一个对象的内部创建的类与对象的UML关系?

如何创建一个类,使其对象之一与VB中另一个类的类型相同?

java - 如何在另一个类中创建一个类的对象,反之亦然?

在另一个类的构造函数中创建n个对象的最佳方法?

接受一个类对象或另一个类对象的方法

创建一个在另一个类创建的点之间画线的类

Android Studio 没有使用另一个类创建对象

创建对象时代码崩溃,因此可以从另一个类调用重绘

如何访问在属于另一个类的静态对象中创建的列表?

在另一个类中创建的对象的C ++数组

在另一个类中创建内部类对象的数组

是否在考虑组合的方法中从另一个类创建对象

当创建另一个类的对象时,Foreach循环崩溃

Python将对象创建附加到另一个类中的列表

在另一个类中创建对象(无继承)