[英]How Can I Use The Value Of A Variable In One Class To Another?
I've defined a variable withdraw
in class Account
.我在
Account
类中定义了一个变量withdraw
。 It performs the withdrawl
function just fine that's defined inside the Account
class.它很好地执行了
Account
类中定义的withdrawl
功能。 However, I wish to access the value of withdraw
variable inside the interest
function present in the Sav_acct
class.但是,我希望访问
Sav_acct
类中存在的interest
函数内的withdraw
变量的值。 It's taking the value of withdraw
as 0. How can I use the value of withdraw
from the withdrawl
function inside the interest
function so I can perform the right mathematical operation?它将
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");
}
}
}
EDIT : Modified the code.But interest is still 0.编辑:修改了代码。但兴趣仍然是 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");
}
}
}
Don't use public static
in a variable just to access it .不要在变量中使用
public static
来访问它。 Use getter
setter
to access variables of a class.使用
getter
setter
访问类的变量。 Thats the right way of doing it.这才是正确的做法。
In your case removing public static
will do fine;在您的情况下,删除
public static
就可以了; In Account
class在
Account
类
public static double withdraw;
to
double withdraw;
You are not maintaining any state while creating object for Account
and Sav_acct
class.You are creating two different objects to do 1 job.在为
Account
和Sav_acct
类创建对象时,您没有维护任何状态。您正在创建两个不同的对象来完成一项工作。 You will never get withdraw
value of Account
in Sav_acct
since both are different objects.您永远不会在
Sav_acct
中获得Account
的withdraw
值,因为两者是不同的对象。
You are not maintaining any state while creating objects for the Account
and Sav_acct
classes.在为
Account
和Sav_acct
类创建对象时,您不会维护任何状态。 You are creating two different objects here.您在这里创建了两个不同的对象。 You will never get the
withdraw
value of Account
in Sav_acct
since both are different objects.您永远不会在
Sav_acct
中获得Account
的withdraw
值,因为两者是不同的对象。 Plus you are missing the basics of Inheritence
in OOPS另外,您缺少 OOPS 中的
Inheritence
基础知识
Since Sav_acct
is extending the Account
class it is inheriting all the properties of Account
, so there is no need of creating an object of the Account
class.由于
Sav_acct
扩展了Account
类,它继承了Account
的所有属性,因此不需要创建Account
类的对象。 Instead, create an object of Sav_acct
and use this obj to perform your operations;相反,创建一个
Sav_acct
对象并使用此 obj 执行您的操作;
Account wi = new Account();
wi.withdrawl();
Sav_acct in = new Sav_acct();
in.interest();
to至
Sav_acct wi = new Sav_acct();
wi.withdrawl();
wi.interest();
You need to know about how Inheritance works in OOPS.您需要了解继承在 OOPS 中是如何工作的。 Refer here
参考这里
You are declaring objects multiple times whenever you 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();
}
}
In the above why create Account de = new Account();
在上面为什么创建
Account de = new Account();
separately for both deposit
and withdraw
just declare one object after getting the account type name and use it to perform respective jobs.分别为
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();
}
}
Redesign your code inside main method otherwise, most of the thing is fine否则在 main 方法中重新设计你的代码,大部分都很好
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.