我在类里面花了两天时间试图弄清楚,但我就是不明白其中的一些错误。
我实际上在这个网站上发现了类似的问题,但我仍然不明白。
类(class)名称为 4.12.4 服装店。
In this problem, you’ll design a few classes that represent different pieces of clothing in a clothing store.
You’ll write the classes for TShirt, Jeans, Sweatshirt and Clothing.
The Clothing class should have two instance variables: one for the size of the clothing (a String), and another for the clothing’s color (also a string).
Clothing should have two accessor (getter methods) as well:
public String getSize() public String getColor()
The Sweatshirt class should have a private instance variable (or field) to store whether or not it has a hood, and a corresponding getter method
public boolean hasHood()
The TShirt class should have a private field to store the fabric and a corresponding getter for that called
public String getFabric()
All Jeans should have the color blue.
The constructors should be of this format:
public Clothing(String size, String color) public TShirt(String size, String color, String fabric) public Sweatshirt(String size, String color, boolean hasHood) public Jeans(String size)
以下是我的代码:
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 class TShirt extends Clothing
{
private String fabric;
public TShirt(String size, String color, String fabric)
{
super(size, color);
this.fabric = fabric;
}
public String getFabric()
{
return fabric;
}
}
public class Sweatshirt extends Clothing
{
private boolean hasHood;
public Sweatshirt(String size, String color, boolean hasHood)
{
super(size, color);
this.hasHood = hasHood;
}
public boolean getHasHood()
{
return this.hasHood;
}
}
public class Jeans extends Clothing
{
public Jeans(String size)
{
super(size);
}
}
我的错误:
Errors: Jeans.java: constructor Clothing in class Clothing cannot be applied to given types;
Grader.java: You may have forgotten to declare hasHood() or it's out of scope
最佳答案
Jeans
仅向 super 构造函数传递一个参数。您没有 Clothing
的单参数构造函数。让 Clothing(String size)
或您的 Jeans
类可以将默认值传递给 super。就像 super(size, "Blue")
或任何合适的东西。
编辑:
将getHasHood()
更改为hasHood()
。您的类(class)正在对您强制执行命名约定。
关于java - 努力学习有关 CodeHS 类和子类的类(class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46899279/