无法在java中读取值继承

时间:2013-06-23 04:21:38

标签: java inheritance

我正在编写一个简单的程序来说明java中的类和继承。该计划是银行,有2个类帐户和操作,其中操作扩展帐户。现在在main中我在Account中声明了值,然后我开始从Operation类调用操作。现在每当Operation调用Account中的值时,它返回0表示int,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));
}

}

这是Operation类:

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

}

}

我真的不知道为什么操作无法读取我添加到帐户的值,因为操作扩展帐户,请您解释一下原因以及如何解决它。 提前谢谢。

1 个答案:

答案 0 :(得分:3)

您有两个单独的对象 - 一个是Account,另一个是Operation

您的Operation类的实例具有名称,ID和金钱 - 但它们与其他帐户或操作实例的名称,ID和金额不同。

修复它的一种方法是向Operation:

添加构造函数
class Operation {
    Operation(String n, int i, double m) {
        super( n, i, m );
    }
    ...
}