我withdraw在 class中定義了一個變數Account。它可以withdrawl很好地執行在Account類中定義的功能。但是,我希望訪問類中存在的函式withdraw內的變數值。它的值為0。如何在函式內部使用 函式的值,以便執行正確的數學運算?interestSav_acctwithdrawwithdrawwithdrawlinterest
匯入 java.util.Scanner;
類帳戶{
String customer_name;
int account_number;
String type_account;
int balance=2500;
double deposit;
雙重退出;
void deposit(){
Scanner sc=new Scanner(System.in);
System.out.println("how much do you want to deposit?");
int amo=sc.nextInt();
deposit= balance amo;
System.out.println("Your current balance is " deposit);
}
無效撤回(){
掃描儀 sc=新的掃描儀(System.in);
System.out.println("你要提現多少?");
int amo=sc.nextInt();
withdraw=balance-amo;
}
}
類 Curr_acct 擴展帳戶{
@SuppressWarnings("空陳述句")
void penalty(){
if(withdraw<2000&& withdraw>0){
double Service_charge=withdraw-100;
double falling= Service_charge;
System.out.println("Your balance is " withdraw);
System.out.println("You've been charged a service charge" " 100" " due to a below the limit balance");
System.out.println("Your current amount is " falling);
}
else if(withdraw<=0){
System.out.println("Your balance is insufficient");
}
else{
System.out.println("Your current balance is " withdraw);
}
}
}
class Sav_acct extends Account{
void interest(){
if(withdraw<0){
System.out.println("Your balance is insufficient");
}
else{
double interests;
interests=(1/100)*withdraw;
double total_amount=interests withdraw;
System.out.println("Your new balance with interest is " total_amount);
}
}
}
公共類發布{
@SuppressWarnings("空陳述句")
公共靜態無效主要(字串[]引數){
掃描儀 sc=新的掃描儀(System.in);
System.out.println("請輸入您的姓名");
String n=sc.nextLine();
System.out.println("Enter your account type: Savings Account or Current Account");
String t=sc.nextLine();
if("Savings Account".equals(t)){
Sav_acct ac =new Sav_acct();
System.out.println("Do you want to deposit or withdraw amount?");
String d=sc.nextLine();
if("deposit".equals(d)){
ac.deposit();
}
else if("withdraw".equals(d)){
ac.withdrawl();
ac.interest();
}
}
else if("Current Account".equals(t)){
Curr_acct ac =new Curr_acct();
System.out.println("Do you want to deposit or withdraw amount?");
String d=sc.nextLine();
if("deposit".equals(d)){
ac.deposit();
}
else if("withdraw".equals(d)){
ac.withdrawl();
ac.penalty();
}
}
else{
System.out.println("please enter the correct account type as per the options provided on the screen");
}
}
}
uj5u.com熱心網友回復:
不要public static在變數中使用只是為了訪問它。用于getter setter訪問類的變數。這才是正確的做法。
在您的情況下,洗掉public static會很好;在Account課堂上
public static double withdraw;
to
double withdraw;
Account在為和 類創建物件時,您沒有保持任何狀態。您Sav_acct正在創建兩個不同的物件來完成一項作業。你永遠不會得到in 的withdraw價值,因為兩者都是不同的物件。AccountSav_acct
Account在為和 Sav_acct類創建物件時,您不會維護任何狀態。您在這里創建了兩個不同的物件。你永遠不會得到 in 的值withdraw,因為它們都是不同的物件。另外,您缺少OOPS的基礎知識AccountSav_acctInheritence
由于Sav_acct是擴展Account類,它繼承了 的所有屬性Account,所以不需要創建Account類的物件。相反,創建一個物件Sav_acct并使用這個 obj 來執行您的操作;
Account wi = new Account();
wi.withdrawl();
Sav_acct in = new Sav_acct();
in.interest();
至
Sav_acct wi = new Sav_acct();
wi.withdrawl();
wi.interest();
您需要了解繼承在 OOPS 中是如何作業的。參考這里
主要問題
您可以在需要時多次宣告物件。例如:-
System.out.println("Enter your account type: Savings Account or Current Account");
String t = sc.nextLine();
if ("Savings Account".equals(t)) {
System.out.println("Do you want to deposit or withdraw amount?");
String d = sc.nextLine();
if ("deposit".equals(d)) {
Account de = new Account(); // creating Account object
de.deposit();
}
else if ("withdraw".equals(d)) {
Account wi = new Account(); // creating Account object again
wi.withdrawl();
Sav_acct in = new Sav_acct();
in.interest();
}
}
在上面為什么要Account de = new Account();為兩者分別創建deposit并withdraw在獲取帳戶型別名稱后宣告一個物件并使用它來執行相應的作業。
System.out.println("Enter your account type: Savings Account or Current Account");
String t = sc.nextLine();
if ("Savings Account".equals(t)) {
Sav_acct acc = new Sav_acct(); //creating sav_acct object here and use
System.out.println("Do you want to deposit or withdraw amount?");
String d = sc.nextLine();
if ("deposit".equals(d)) {
// Account de = new Account(); no need
acc.deposit();
}
else if ("withdraw".equals(d)) {
//Account wi = new Account(); no need
acc.withdrawl();
//Sav_acct in = new Sav_acct(); no need again
acc.interest();
}
}
否則在 main 方法中重新設計你的代碼,大部分都很好
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/483877.html
上一篇:基于具有不同屬性的一個類呼叫物件
