gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 19:33:28 25 4
gpt4 key购买 nike

我正在尝试使用 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);
}

这相当于在商店购买杂货但忘记带回家的编程。不要这样做,而是记得把你的元素带回家。

关于java - 无法从另一个类创建类对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58667313/

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