[英]How Can I Use The Value Of A Variable In One Class To Another?
我在Account
類中定義了一個變量withdraw
。 它很好地執行了Account
類中定義的withdrawl
功能。 但是,我希望訪問Sav_acct
類中存在的interest
函數內的withdraw
變量的值。 它將withdraw
的值設為 0。如何在interest
函數中使用withdrawl
函數中的withdraw
值,以便執行正確的數學運算?
public class Account {
String customer_name;
int account_number;
String type_account;
int balance = 2500;
double deposit;
public static double withdraw;
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 " + balance);
}
void withdrawl() {
Scanner sc = new Scanner(System.in);
System.out.println("how much do you want to withdraw?");
int amo = sc.nextInt();
withdraw = balance - amo;
if (withdraw < 0) {
System.out.println("Your balance is insufficient");
}
else {
System.out.println("Your current balance is " + withdraw);
}
}
}
class Curr_acct extends Account {
@SuppressWarnings("empty-statement")
void penalty() {
double Service_charge = withdraw - 100;
double falling = Service_charge;
System.out.println("You've been charged" + " 100" + " due to low balance");
System.out.println("Your current amount is " + falling);
}
}
class Sav_acct extends Account {
void interest() {
double interest;
interest = (1 % 100) * withdraw;
double total_amount = interest + withdraw;
System.out.println("Your new balance with interest is " + total_amount);
}
}
class publish {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name");
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)) {
System.out.println("Do you want to deposit or withdraw amount?");
String d = sc.nextLine();
if ("deposit".equals(d)) {
Account de = new Account();
de.deposit();
}
else if ("withdraw".equals(d)) {
Account wi = new Account();
wi.withdrawl();
Sav_acct in = new Sav_acct();
in.interest();
}
}
else if ("Current 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();
de.deposit();
}
else if ("withdraw".equals(d)) {
Account wi = new Account();
wi.withdrawl();
if (withdraw < 2000) {
Curr_acct pe = new Curr_acct();
pe.penalty();
}
else {
}
}
}
else {
System.out.println("please enter the correct account type as per the options provided on the screen");
}
}
}
編輯:修改了代碼。但興趣仍然是 0。
import java.util.Scanner;
class Account {
String customer_name;
int account_number;
String type_account;
int balance = 2500;
double deposit;
double withdraw;
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);
}
void withdrawl() {
Scanner sc = new Scanner(System.in);
System.out.println("how much do you want to withdraw?");
int amo = sc.nextInt();
withdraw = balance - amo;
}
}
class Curr_acct extends Account {
@SuppressWarnings("empty-statement")
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);
}
}
}
public class publish {
@SuppressWarnings("empty-statement")
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name");
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");
}
}
}
不要在變量中使用public static
來訪問它。 使用getter
setter
訪問類的變量。 這才是正確的做法。
在您的情況下,刪除public static
就可以了; 在Account
類
public static double withdraw;
to
double withdraw;
在為Account
和Sav_acct
類創建對象時,您沒有維護任何狀態。您正在創建兩個不同的對象來完成一項工作。 您永遠不會在Sav_acct
中獲得Account
的withdraw
值,因為兩者是不同的對象。
在為Account
和Sav_acct
類創建對象時,您不會維護任何狀態。 您在這里創建了兩個不同的對象。 您永遠不會在Sav_acct
中獲得Account
的withdraw
值,因為兩者是不同的對象。 另外,您缺少 OOPS 中的Inheritence
基礎知識
由於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 方法中重新設計你的代碼,大部分都很好
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.