简体   繁体   中英

How to return an abstract type?

Okay, so I have an abstract class named Product, and two classes Clothing and Food that inherit from Product. I also have a third class

public class ProductParser
{
public static Product parseStringToProduct(String lineToParse)
{

    String[] inputToken = lineToParse.split("/");
    if (inputToken[0].equalsIgnoreCase("Food"))
    {
        = new Food(inputToken[1], Integer.parseInt(inputToken[2]), Double.parseDouble(inputToken[3]), inputToken[4], Double.parseDouble(inputToken[5]), inputToken[6]);

    }
    else
    {
        = new Clothing(inputToken[1], Integer.parseInt(inputToken[2]), Double.parseDouble(inputToken[3]), inputToken[4], inputToken[5]);
    }

}

}

This method will parse a string, pull out the information, create a new Clothing or Food object using their constructor with attributes of the object, and return it to the calling method.

I am running into troubles when trying to return the object. The IDE is telling me I need to return a result of type Product, but the Product class is abstract and cannot be instantiated.

How am I supposed to create these objects and return them?

EDIT: I left out the return keywords on purpose because I thought I was supposed to create a variable or object that I then return. I understand now that Class and Food are inherently (whole concept of inheritance silly me) Products and can be simply returned as such. Thank you all for your answers. It seems so obvious now...

This is basic case of polymorphism, where Food or clothing can be returned as a Product , because Food ISA product and Clothing ISA product

what you want to do, it can be done like this:

public static Product parseStringToProduct(String lineToParse)
{
    Product p=null;
    String[] inputToken = lineToParse.split("/");
    if (inputToken[0].equalsIgnoreCase("Food"))
    {
       p = new Food(inputToken[1], Integer.parseInt(inputToken[2]), Double.parseDouble(inputToken[3]), inputToken[4], Double.parseDouble(inputToken[5]), inputToken[6]);

    }
    else
    {
       p = new Clothing(inputToken[1], Integer.parseInt(inputToken[2]), Double.parseDouble(inputToken[3]), inputToken[4], inputToken[5]);
    }
    return p;
}

or instead of assigning it to a product variable, you can directly return it from if-else block

hope this helps!

Good luck!

See if this helps:-

          String[] inputToken = lineToParse.split("/");
          Product product = null;
          if (inputToken[0].equalsIgnoreCase("Food"))
          {
              product = new Food(inputToken[1], Integer.parseInt(inputToken[2]), Double.parseDouble(inputToken[3]), inputToken[4], Double.parseDouble(inputToken[5]), inputToken[6]);

          }
          else
          {
              product = new Clothing(inputToken[1], Integer.parseInt(inputToken[2]), Double.parseDouble(inputToken[3]), inputToken[4], inputToken[5]);
          }
         return product;

You forgot the return keyword:

public class ProductParser
{
public static Product parseStringToProduct(String lineToParse)
{

    String[] inputToken = lineToParse.split("/");
    if (inputToken[0].equalsIgnoreCase("Food"))
    {
       return new Food(inputToken[1], Integer.parseInt(inputToken[2]), Double.parseDouble(inputToken[3]), inputToken[4], Double.parseDouble(inputToken[5]), inputToken[6]);

    }
    else
    {
        return new Clothing(inputToken[1], Integer.parseInt(inputToken[2]), Double.parseDouble(inputToken[3]), inputToken[4], inputToken[5]);
    }

}

You need to extend Product class and create a concrete class before you can instantiate it.

Since Clothing and Food are types of Product , you can just assign it to a variable of type Product .

In fact, here you can just return them without declaring a variable. For example:

return new Clothing(inputToken[1], Integer.parseInt(inputToken[2]), Double.parseDouble(inputToken[3]), inputToken[4], inputToken[5]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM