0

我正在编写一个简单的程序来说明 Java 中的类和继承。该程序是具有 2 个类 Account 和 Operation 的 Bank,其中 Operation 扩展了 Account。现在主要我在 Account 中声明了值,然后我开始从 Operation 类中调用操作。现在,每当 Operation 调用 Account 中的值时,它都会为 int 返回 0,为 string 返回 null,尽管我为其添加了值。

这是帐户:

public class Account {

 private String Name;
 private int ID;
 private double Money;

Account()
{

}
Account (String n, int i, double m)
{
    Name = n;
    ID = i;
    Money = m;
}

public void setMoney(double m)
{
    Money = m;
}

public double getMoney()
{    
    return Money;
}

public int getID()
{
    return ID;
}

public String getName()
{
    return Name;
}

public void Check()
{
    System.out.println("Your Name is: " + Name + " Your ID is: " + String.valueOf(ID) + " Your Balance is: " + String.valueOf(Money));
}

}

这是操作类:

import java.util.Date;


public class Operation extends Account{

private Date TransactionDate;


public void Withdraw(double amount)
{
    double Money = getMoney();
    if (amount > Money)
        System.out.println("The amount is insufficient");
    else
    {
        Money = Money - amount;
        TransactionDate = new Date();
        setMoney(Money);
        System.out.println("The available money is " + String.valueOf(Money) + " at " + String.valueOf(TransactionDate));

    }
}

public void Deposit(double amount)
{
    double Money = getMoney();
    if (amount < 0)
        System.out.println("You cant add minus");
    else
    {
        Money = Money + amount;
        TransactionDate = new Date();
        setMoney(Money);
        System.out.println("Your credits are: " + String.valueOf(Money) + " at " + String.valueOf(TransactionDate));
    }
}

public void Check()
{
    String Name = getName();
    int ID = getID();
    double Money = getMoney();

    System.out.println("Your Name is: " + Name + " Your ID is: " + String.valueOf(ID) + " Your Balance is: " + String.valueOf(Money));
}
}

这是主要的:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

 public class Main {

/**
 * @param args
 * @throws IOException
 * @throws NumberFormatException
 */
public static void main(String[] args) throws NumberFormatException,
        IOException {
    // TODO Auto-generated method stub

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    System.out.println("Enter your ID");
    int ID = Integer.parseInt(br.readLine());
    System.out.println("Enter your Name");
    String Name = br.readLine();
    System.out.println("Enter your Money");
    double Money = Double.parseDouble(br.readLine());


    Account a = new Account(Name, ID, Money);
    a.Check();

    Operation o = new Operation();


    while (true) {
        System.out.println("\n-----------------------------------");
        System.out
                .println("Enter 1 to withdraw, 2 to Deposit, 3 to Check, 4 to exit ");
        int operation = Integer.parseInt(br.readLine());
        switch (operation) {
        case 1:
            System.out.println("enter the amount");
            double withdrawMoney = Double.parseDouble(br.readLine());
            o.Withdraw(withdrawMoney);
            break;
        case 2:
            System.out.println("enter the amount");
            double Depositmoney = Double.parseDouble(br.readLine());
            o.Deposit(Depositmoney);
            break;
        case 3:
            o.Check();
            break;
        case 4:
            break;
        default:
            System.out.println("enter between 1 and 4 only");
        }

        if (operation == 4)
            break;
    }

}

}

我真的不知道为什么 Operation 无法读取我添加到 Account 的值,因为 Operation extends Account 可以请您向我解释原因以及如何解决它。提前致谢。

4

1 回答 1

3

你有两个独立的对象——一个 an Account,一个 an Operation

您的 Operation 类的实例具有名称、ID 和货币 - 但它们与 Account 或 Operation 的其他实例不同的名称、ID 和货币。

解决它的一种方法是将构造函数添加到操作:

class Operation {
    Operation(String n, int i, double m) {
        super( n, i, m );
    }
    ...
}
于 2013-06-23T04:24:46.520 回答