如何通过数组对象传递参数?

时间:2014-10-30 15:54:45

标签: java arrays object parameters

我遇到的问题是,当我创建5个OutgoingPhoneCall对象时,第二个参数不会通过。我无法弄明白为什么。任何帮助将不胜感激。

public class PhoneCallArray {

public static void main(String[] args) {

        PhoneCall [] phoneArray = new PhoneCall[10];

        for(int x = 0; x < 5; x++)
        {
            phoneArray[x] = new IncomingPhoneCall("1-800-555-789" + x);
        }

        for(int y = 5; y < 10; y++)
        {
            phoneArray[y] = new OutgoingPhoneCall("1-800-555-789" + y, y * 5);
        }

        for(int a = 0; a < 10; a++)
        {
            phoneArray[a].displayInfo();
        }

public abstract class PhoneCall {

    //Declare variables.
    String phoneNumber;
    double callPrice;

    //Constructor.
    public PhoneCall(String phoneNumber){
        this.phoneNumber = phoneNumber;
        callPrice = 0.0;
    }

    public void setCallPrice(double callPrice) {
        this.callPrice = callPrice;
    }

    public abstract String getPhoneNumber();
    public abstract double getPrice();
    public abstract void displayInfo();
}


public class OutgoingPhoneCall extends PhoneCall {

    int callTime;

    public OutgoingPhoneCall(String phoneNum, int time){

        super(phoneNum);
        time = callTime;
        callPrice = 0.04;
    }

    @Override
    public String getPhoneNumber(){
        return phoneNumber;
    }

    @Override
    public double getPrice(){
        callPrice = callTime * callPrice;
        return callPrice;
    }

    public int getCallTime(){
        return callTime;
    }

    @Override
    public void displayInfo() {

        JOptionPane.showMessageDialog(null, "Phone number: " + getPhoneNumber() + 
                            "\nRate: " + callPrice +
                            "\nTotal duration: " + getCallTime() + " min"+
                            "\nPrice of call: $" + getPrice(),"Call Summary",
                            JOptionPane.INFORMATION_MESSAGE); 
    }
}

1 个答案:

答案 0 :(得分:1)

你想:

callTime = time;

time = callTime;

在你的构造函数中。你让它们翻转过来。