Java 使用默认构造函数
Java using default constructor
我在我的程序中遇到了构造函数的小问题。它是一个简单的银行数据库,用于存储客户数据。我必须实现在两个账户之间存入、提取和转移现金的方法。我已经实现了那种构造函数来添加新的银行账户:
public Customer() {
Scanner scan = new Scanner(System.in);
System.out.println("Enter id of customer:");
this.id = scan.nextLine();
File folder = new File("CustomerDataBase" + File.separator + this.id + ".txt");
if(folder.exists()) {
System.out.println("Customer ID already exists!");
System.exit(1);
}
try {
System.out.println("Enter name of customer:");
this.name = scan.nextLine();
System.out.println("Enter surname of customer:");
this.surname = scan.nextLine();
System.out.println("Enter PESEL number of customer:");
this.pesel = scan.nextLine();
System.out.println("Enter address of customer:");
System.out.println(" Street:");
this.adressStreet = scan.nextLine();
System.out.println(" City:");
this.adressCity = scan.nextLine();
System.out.println(" Zip Code: ");
this.zipCode = scan.nextLine();
System.out.println("Enter funds of Customer:");
this.funds = Double.parseDouble(scan.nextLine());
this.saveCustomer();
scan.close();
}catch(NumberFormatException e) {
System.out.println("Error : " + e);
System.exit(1);
}
}
然后我有一个方法 withdraw
:
static void withdraw (int amount, int id) {
File f = new File("CustomerDataBase" + File.separator + String.valueOf(id) + ".txt");
Scanner fRead;
Customer tempCustomer = new Customer();
try{
fRead = new Scanner(f);
tempCustomer.id = fRead.nextLine();
tempCustomer.name = fRead.nextLine();
tempCustomer.surname = fRead.nextLine();
tempCustomer.pesel = fRead.nextLine();
tempCustomer.adressStreet = fRead.nextLine();
tempCustomer.adressCity = fRead.nextLine();
tempCustomer.zipCode = fRead.nextLine();
tempCustomer.funds = Double.parseDouble(fRead.nextLine()) - id;
fRead.close();
tempCustomer.saveCustomer();
}catch(FileNotFoundException e) {
System.out.println("File not found!");
System.exit(1);
}
}
withdraw
方法从文件中读取数据并将其存储在 class 中。所以我正在创建对象作为客户类型。但是我只想使用 "plain" (默认)构造函数,当您不声明自己的构造函数时, Java 会提供该构造函数。
怎么做?我阅读了有关 super():
的声明,但如果我理解正确,它仅在您从另一个 class.
继承时才有效
每个 class 都带有一个在 class 本身中不可见的默认构造函数。 但是,请注意,如果您指定默认构造函数以外的构造函数,则不能使用默认构造函数,具体如下@Rustam 评论。例如,假设您的客户 class 看起来像这样:
public class Customer {
private String name;
private String lastName;
private age int;
private String ssn;
//default constructor that is NOT visible
Customer()
{
}
//other constructor given name and lastName
Customer(name, lastName)
{
this.name = name;
this.lastName = lastName;
}
//getters and setters
}
构造函数 Customer() 是默认创建的,您无需在 class 中包含。
然后您可以使用默认构造函数创建一个客户实例,然后您需要使用 setter 来设置属性,如下所示:
public class Test {
public static void main(String [] args){
Customer c = new Customer();
//setting parameters
c.setName("Jose");
c.setLastName("Mejia");
}
}
我在我的程序中遇到了构造函数的小问题。它是一个简单的银行数据库,用于存储客户数据。我必须实现在两个账户之间存入、提取和转移现金的方法。我已经实现了那种构造函数来添加新的银行账户:
public Customer() {
Scanner scan = new Scanner(System.in);
System.out.println("Enter id of customer:");
this.id = scan.nextLine();
File folder = new File("CustomerDataBase" + File.separator + this.id + ".txt");
if(folder.exists()) {
System.out.println("Customer ID already exists!");
System.exit(1);
}
try {
System.out.println("Enter name of customer:");
this.name = scan.nextLine();
System.out.println("Enter surname of customer:");
this.surname = scan.nextLine();
System.out.println("Enter PESEL number of customer:");
this.pesel = scan.nextLine();
System.out.println("Enter address of customer:");
System.out.println(" Street:");
this.adressStreet = scan.nextLine();
System.out.println(" City:");
this.adressCity = scan.nextLine();
System.out.println(" Zip Code: ");
this.zipCode = scan.nextLine();
System.out.println("Enter funds of Customer:");
this.funds = Double.parseDouble(scan.nextLine());
this.saveCustomer();
scan.close();
}catch(NumberFormatException e) {
System.out.println("Error : " + e);
System.exit(1);
}
}
然后我有一个方法 withdraw
:
static void withdraw (int amount, int id) {
File f = new File("CustomerDataBase" + File.separator + String.valueOf(id) + ".txt");
Scanner fRead;
Customer tempCustomer = new Customer();
try{
fRead = new Scanner(f);
tempCustomer.id = fRead.nextLine();
tempCustomer.name = fRead.nextLine();
tempCustomer.surname = fRead.nextLine();
tempCustomer.pesel = fRead.nextLine();
tempCustomer.adressStreet = fRead.nextLine();
tempCustomer.adressCity = fRead.nextLine();
tempCustomer.zipCode = fRead.nextLine();
tempCustomer.funds = Double.parseDouble(fRead.nextLine()) - id;
fRead.close();
tempCustomer.saveCustomer();
}catch(FileNotFoundException e) {
System.out.println("File not found!");
System.exit(1);
}
}
withdraw
方法从文件中读取数据并将其存储在 class 中。所以我正在创建对象作为客户类型。但是我只想使用 "plain" (默认)构造函数,当您不声明自己的构造函数时, Java 会提供该构造函数。
怎么做?我阅读了有关 super():
的声明,但如果我理解正确,它仅在您从另一个 class.
每个 class 都带有一个在 class 本身中不可见的默认构造函数。 但是,请注意,如果您指定默认构造函数以外的构造函数,则不能使用默认构造函数,具体如下@Rustam 评论。例如,假设您的客户 class 看起来像这样:
public class Customer {
private String name;
private String lastName;
private age int;
private String ssn;
//default constructor that is NOT visible
Customer()
{
}
//other constructor given name and lastName
Customer(name, lastName)
{
this.name = name;
this.lastName = lastName;
}
//getters and setters
}
构造函数 Customer() 是默认创建的,您无需在 class 中包含。
然后您可以使用默认构造函数创建一个客户实例,然后您需要使用 setter 来设置属性,如下所示:
public class Test {
public static void main(String [] args){
Customer c = new Customer();
//setting parameters
c.setName("Jose");
c.setLastName("Mejia");
}
}