gpt4 book ai didi

Java 没有显示我正在寻找的输出(初学者)

转载 作者:行者123 更新时间:2023-12-02 12:10:54 25 4
gpt4 key购买 nike

大家好,我对 Java 相当陌生,但我遇到了一个似乎无法解决的问题,问题出在“WashingMachine”类中,它没有显示“spinSpeed”详细信息,任何答案将不胜感激

这是我的代码:

enter code here

package test;

public class Client {
private String Name;
private String PhoneNo;

Client () {
Name = null;
PhoneNo= null;
}
Client (String N, String P){
Name = N;
PhoneNo = P;
}

public void setName(String N){
Name = N;
}
public void setPhoneNo(String P) {
PhoneNo = P;
}

public String getName(){
return Name;
}
public String setPhoneNo() {
return PhoneNo;
}

public String toString() {
return "\nName: "+ Name + "\nPhoneNo:"+ PhoneNo.toString();
}

}

enter code here

封装测试;

公开课机器{

private String Make;
private double Price;
private Client Cust;

public Machine(String make, double price, Client cust)
{
Make = make;
Price = price;
Cust = cust;
}

@Override
public String toString() {
return "\n" +"Make of machine: " + Make + "\n" + "Price: " + Price + "\n" + Cust.toString();
}

public String getMake() {
return Make;
}

public double getprice() {
return Price;
}

public Client getcust() {
return Cust;
}

}

enter code here
package test;

public class WashingMachine extends Machine {
private int spinSpeed;

public WashingMachine (String make, double price, Client cust, int spinSpeed){
super(make, price, cust);
}

@Override
public String toString() {
return "WashingMachine [spinSpeed=" + spinSpeed + ", spinSpeed()=" + spinSpeed() + "]";
}

public int spinSpeed() {
return spinSpeed;
}



enter code here
package test;

import java.util.ArrayList;

public class MachinePurchaseTestVerC {
public static void main(String [] args) {
ArrayList<Machine> gadgets = new ArrayList<Machine> ();
Client mCust2 = new Client("Paul", "0487654321");
Client mCust3 = new Client("Chandra", "0487651234");
Client wCust1 = new Client("Catherine", "0412345678");
Client wCust4 = new Client("Mike", "0412348756");

gadgets.add(new WashingMachine("Bosch", 549.50, wCust1, 3500));
gadgets.add(new Machine("Samsung", 678.50, mCust2));
gadgets.add(new Machine("Electrolux", 449.25, mCust3));
gadgets.add(new WashingMachine("LG", 500.00, wCust4, 3200));

for(int i = 0; i<gadgets.size(); i++){
System.out.println(gadgets.get(i).toString());
System.out.println("----------------------------------");
}
}

}

最佳答案

在您的 WashingMachine 类中,您忘记在构造函数中设置速度

public WashingMachine (String make, double price, Client cust, int spinSpeed){
super(make, price, cust);
this.spinSpeed = spinSpeed;
}

希望有帮助:)

关于Java 没有显示我正在寻找的输出(初学者),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46556429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com