Java 如何通过数组对象传递参数?
我遇到的问题是,当我创建5个OutgoingPhoneCall对象时,第二个参数没有通过。但我不明白为什么。任何帮助都将不胜感激Java 如何通过数组对象传递参数?,java,arrays,object,parameters,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++) {
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);
}
}
公共类PhoneCallArray{
公共静态void main(字符串[]args){
PhoneCall[]phoneArray=新的PhoneCall[10];
对于(int x=0;x<5;x++)
{
phoneArray[x]=新入局电话呼叫(“1-800-555-789”+x);
}
对于(int y=5;y<10;y++)
{
phoneArray[y]=新的外出电话呼叫(“1-800-555-789”+y,y*5);
}
对于(int a=0;a<10;a++)
{
phoneArray[a].displayInfo();
}
公共抽象类电话呼叫{
//声明变量。
字符串电话号码;
双倍买入价;
//构造器。
公用电话呼叫(字符串电话号码){
this.phoneNumber=电话号码;
callPrice=0.0;
}
公开作废setCallPrice(双重callPrice){
this.callPrice=callPrice;
}
公共抽象字符串getPhoneNumber();
公共抽象双getPrice();
公共摘要void displayInfo();
}
公共类OutgoingPhoneCall扩展PhoneCall{
int调用时间;
公共支出电话呼叫(字符串phoneNum,整数时间){
超级(phoneNum);
时间=通话时间;
callPrice=0.04;
}
@凌驾
公共字符串getPhoneNumber(){
返回电话号码;
}
@凌驾
公开双价{
callPrice=callTime*callPrice;
退货价格;
}
public int getCallTime(){
返回调用时间;
}
@凌驾
public void displayInfo(){
JOptionPane.showMessageDialog(空,“电话号码:+getPhoneNumber()+
“\n利率:”+callPrice+
\n总持续时间:“+getCallTime()+”分钟+
“\n调用价格:$”+getPrice(),“调用摘要”,
JOptionPane.INFORMATION(信息和消息);
}
}
您需要:
callTime = time;
不是
在你的构造函数中。你已经把它们翻了一遍。成功了!谢谢,我想我只是编码太快了,没有意识到。哈哈,这是真的……很高兴我能帮上忙!你能标记为正确吗?一些简单的调试就可以找到问题的原因。上网查找答案可能会花费你更多的时间,而且没有教你答案宝贵的技能
time = callTime;