java - 努力学习有关 CodeHS 类和子类的类(class)

标签 java

我在类里面花了两天时间试图弄清楚,但我就是不明白其中的一些错误。

我实际上在这个网站上发现了类似的问题,但我仍然不明白。

类(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/

相关文章:

java - InterceptSendToEndpoint不拦截Route中的http请求,使用Camel和Springboot

java - 如何获取 XSOM Java 库中的 Use Attribute

java - 具有包含 HTML 代码的 JLabel 节点的 JTree 在展开节点时抛出异常

java - 我想使用回收器 View 从 firebase 获取数据到内部 fragment

java - 更新后单击 Vector Assets 上的颜色时 Android Studio 崩溃

java - 即使测试用例失败也执行 Selenium 脚本

java - 如何使用 Scanner 读取文件并将其传递给 Java 中的方法

java - 代码重复(或不重复)- JAVA

java - 如何使用 Kotlin 和 jackson ObjectMapper 从 json 中删除属性

java - 算法将数字转换为字符串