gpt4 book ai didi

java - Constructor Jeans 不能应用给定的类型

转载 作者:行者123 更新时间:2023-11-29 08:24:33 25 4
gpt4 key购买 nike

我在 CodeHS 中工作,我收到此错误:类 Jeans 中的构造函数 Jeans 无法应用于给定类型;

我看过了,但找不到解决这个问题的方法。

我试过只使用父类(super class)中的一个参数,然后是所有参数,然后是所有参数再加上我创建的一个。牛仔裤必须是蓝色的,因此我只想说尺码。

这是父类(super class):

public class Clothing
{
public String size;
public String color;

public Clothing(String size, String color)
{
this.size = size;
this.color = color;
}

public String getSize()
{
return size;
}

public String getColor()
{
return color;
}

public String toString()
{
return "Clothing with size " + size + " and the color " + color;
}
}

这是 Jeans 类:

public class Jeans extends Clothing
{

public Jeans(String size)
{
super(size);
}

public String toString()
{
return "Blue jeans with size " + size;
}
}

这是我正在测试的编码:

public class ClothingTester extends ConsoleProgram
{
public void run()
{
Clothing myClothes = new Clothing("24", "grey");
System.out.println(myClothes);
Sweatshirt mySweatshirt = new Sweatshirt("24", "grey", true);
System.out.println(mySweatshirt.getSize());
TShirt myTShirt = new TShirt("24", "grey", "polyester");
System.out.println(myTShirt);
Jeans myJeans = new Jeans("24");
System.out.println(myJeans);
}
}

这是我必须使用父类(super class) Clothing 的另外两个子类(它们工作正常):

public class TShirt extends Clothing
{
public String fabric;

public TShirt(String size, String color, String fabric)
{
super(size, color);
this.fabric = fabric;
}

public String getFabric()
{
return fabric;
}

public String toString()
{
return "T-shirt with size " + size + " and is the color " + color + " and is made of " + fabric;
}
}

public class Sweatshirt extends Clothing
{
public boolean hasHood;

public Sweatshirt(String size, String color, boolean hasHood)
{
super(size, color);
this.hasHood = hasHood;
}

public boolean hasHood()
{
return hasHood;
}

public String toString()
{
return "Sweatshirt with size " + size + " and is the color " + color + ". Does it have a hood? " + hasHood;
}
}

我希望 Jeans 输出尺寸(颜色应保持蓝色),但我收到编译时错误。

最佳答案

您的 Jeans 类没有为父类(super class)提供 color 参数。

public Jeans(String size)
{
/* Default color for Jeans is blue. */
super(size, "blue");
}

关于java - Constructor Jeans 不能应用给定的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54257974/

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