如何将一个 Class 中变量的值用于另一个?

How Can I Use The Value Of A Variable In One Class To Another?

我在 class Account 中定义了一个变量 withdraw。它可以很好地执行在 Account class 中定义的 withdrawl 函数。但是,我希望在 Sav_acct class 中的 interest 函数中访问 withdraw 变量的值。它将 withdraw 的值设为 0。如何在 interest 函数中使用 withdrawl 函数中的 withdraw 的值,以便执行正确的数学运算?

进口java.util.Scanner;

class 帐户{

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;
               
            

}

}

class 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);
            }

}

}

public class 发布{

@SuppressWarnings("空语句")

public static void main(String[] args) {

扫描仪 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");
        
    }
    
   
    
}
}

不要为了访问变量而在变量中使用 public static。使用 getter setter 访问 class 的变量。这才是正确的做法。

在你的情况下删除public static就可以了; 在 Account class

public static double withdraw; 

to 

double withdraw;

您在为 Account 创建对象时没有保持任何状态,而 Sav_acct class.You 正在创建两个不同的对象来完成一项工作。 您永远不会在 Sav_acct 中获得 Accountwithdraw 值,因为两者是不同的对象。

您在为 AccountSav_acct class 对象创建对象时没有维护任何状态。您在这里创建了两个不同的对象。 您永远不会在 Sav_acct 中获得 Accountwithdraw 值,因为两者是不同的对象。另外,您还缺少 OOPS

Inheritence 的基础知识

由于 Sav_acct 扩展了 Account class 它继承了 Account 的所有属性,因此不需要创建 [=] 的对象19=] class。 相反,创建一个 Sav_acct 的对象并使用此对象执行您的操作;

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 中的工作原理。 Refer here

主要问题

每当您 needed.Eg:-

时,您就在多次声明对象
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();
    }
}

在上面为什么要为depositwithdraw分别创建Account de = new Account();只是在获取帐户类型名称后声明一个对象并使用它来执行相应的工作。

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 方法中的代码,否则,大部分都没有问题