我正在CodeHS中工作,但出现此错误:无法将类Jeans中的构造器Jeans应用于给定类型;
我已经看过了,但是找不到解决该问题的方法。
我尝试仅使用超类中的一个参数,然后全部使用,再加上我创建的所有参数。牛仔裤必须是蓝色,这就是为什么我要试着称呼尺寸。
这是超类:
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;
}
}
这是牛仔裤课:
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);
}
}
在这里,我还必须使用超级类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输出大小(颜色应保持蓝色),但是会出现编译器时间错误。
答案 0 :(得分:2)
您的Jeans类没有为超级类提供color
参数。
public Jeans(String size)
{
/* Default color for Jeans is blue. */
super(size, "blue");
}
答案 1 :(得分:2)
Jeans
正在扩展Clothing
。在您的Jeans构造函数中,您尝试在Clothing
类中调用构造函数。但是,您仅用一个参数调用了一个构造函数:super(size);
Clothing中没有单参数构造函数。
有几种方法可以解决此问题。
选项1:默认牛仔裤的“颜色”。
public Jeans(String size) {
super( size, "Blue" );
}
选项2:向服装添加一个单参数构造函数。
public Clothing(String size) {
this.size = size;
this.color = "Blue"; // defaults all Clothing to blue unless otherwise specified
}
选项3:向Jeans构造函数添加第二个参数,并将其传递给Clothing。
public Jeans(String size, String color) {
super(size, color);
}
答案 2 :(得分:0)
这是因为Clothes类没有仅带有参数Size的构造函数。
您可以将一个构造函数添加到Clothes:
public Clothing(String size)
{
this.size = size;
}
或者您更改Jeans的构造函数:
public Jeans(String size, String color)
{
super(size, color);
}
答案 3 :(得分:0)
欢迎来到!
您遇到的问题在此行中:
super(size);
因为类Clothing
中没有单参数构造函数。由于您知道Jeans
是蓝色的,因此您需要使用以下两个参数的构造函数:
public Jeans(String size)
{
super(size,"blue");
}
或者,也许更好:
private final String COLOR = "blue";
public Jeans(String size)
{
super(size,COLOR);
}
答案 4 :(得分:0)
你需要像在 Tshirt 中一样初始化服装中的父实例变量 您需要使用超级键盘传递颜色参数 比如
public Jeans(String size, String color)
{
super(size, color);
}
super 会调用你的父构造函数并初始化父实例变量。