Constructor Jeans cannot apply given types

问题: I'm working in CodeHS, and I get this error: constructor Jeans in class Jeans cannot be applied to given types; I've looked, but I can't find how to solve this issue. I...

问题:

I'm working in CodeHS, and I get this error: constructor Jeans in class Jeans cannot be applied to given types;

I've looked, but I can't find how to solve this issue.

I've tried using just one of the parameters from the superclass, then all of them, then all plus one I created. The jeans have to be the color blue, hence why I'm trying to just call size.

Here's the 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;
    }
}

Here's the Jeans class:

public class Jeans extends Clothing
{

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

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

Here's the coding I'm testing:

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);
    }
}

Here's two other subclasses I had to use super class Clothing for(these work fine):

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;
    }
}

I expect Jeans to output the size (the color should stay blue), but I get a compiler time error.


回答1:

Your Jeans class doesn't provide the color argument for the super class.

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

回答2:

Jeans is extending Clothing. In your Jeans constructor, you're attempting to call a constructor in the Clothing class. However, you've called a constructor with only one argument: super(size); There is no single-argument constructor in Clothing.

There are several ways you can fix this.

Option 1: Default the "color" in Jeans.

public Jeans(String size) {
    super( size, "Blue" );
}

Option 2: Add a single-argument constructor to Clothing.

public Clothing(String size) {
    this.size = size;
    this.color = "Blue"; // defaults all Clothing to blue unless otherwise specified
}

Option 3: Add a second argument to the Jeans constructor, and pass that to Clothing.

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

回答3:

It is because class Clothes does not have a constructor with argument Size only.

You could either add a constructor to Clothes:

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

Or you change the constructor of Jeans:

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

回答4:

welcome to SO!

The problem you're having is in this line:

super(size);

because there's no one-argument constructor in class Clothing. Since you know Jeans are blue, you need to use the two-argument constructor like this:

public Jeans(String size)
{
  super(size,"blue");
}

Or, maybe better:

private final String COLOR = "blue";
public Jeans(String size)
{
  super(size,COLOR);
}
  • 发表于 2019-01-20 23:32
  • 阅读 ( 225 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除