私のプログラムでコンストラクタに少し問題がありました。これは、顧客データを格納する単純な銀行データベースです。私は2つの口座の間で現金を預け入れ、引き出し、移すための方法を実装しなければならない。店舗にデフォルトのコンストラクタを使用するJava
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
方法は、ファイルからデータを読み込み、持っている:私は、新しい銀行口座を追加するには、コンストラクタのようなもの実装しました:
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
を持っていますそれはクラスで。だから私は顧客のタイプとしてオブジェクトを作成しています。しかし、私は自分が宣言していないときにJavaが提供する "普通の"(デフォルトの)コンストラクタを使いたいと思っています。 どうすればいいですか?私はsuper():
のステートメントについて読んでいますが、正しく理解すれば、別のクラスから継承するときにのみ動作します。
あなた 'customer'クラスは、独自のコンストラクタを定義した:
あなたは、デフォルトのコンストラクタを使用して、顧客のインスタンスを作成することができ、その後、あなたは次のように、プロパティを設定するためにセッターを使用する必要がありますデフォルトのプレーンは作成されません... –
彼が宣言したコンストラクタは、引数なしのコンストラクタとして宣言されているため、デフォルトのコンストラクタです – pczeus
これは、 "プレーンな" –