抽象类返回

时间:2018-04-25 03:36:17

标签: java oop inheritance

对不起,如果这是小事。我创建了一个带有一些子类的抽象类。控制器类创建一个类型为所请求子类类型的抽象类的对象,并返回实现的抽象类。 子类具有特定的属性。我无法访问返回对象的那些属性,因为这是抽象类的默认类型,所以我尝试了转换。但这会产生错误" item.Default无法转换为item.cloth"

如何解决?

代码:

public class testMain {

    public void main(int id) {
    Product test;

    switch(ProductController.getCategory(id)) {
    case "Cloth":
        test = (cloth) ProductController.getProduct(id);
        break;
    case "Wear":
        test = (wear) ProductController.getProduct(id);
        break;
    default:
        test = (Default) ProductController.getProduct(id);
    }

    System.out.println("Product No : " + test.ProductNo);
    System.out.println("Title : " + test.Title);
    System.out.println("Description : " + test.Desc);
    System.out.println("Short Description : " + test.ShortDesc);
    System.out.println("Regular Price : " + test.RegularPrice);
    System.out.println("Sale Price : " + test.SalePrice);
    System.out.println("Category : " + test.Category);

    if(((String) test.Category).split(",")[1].contentEquals("cloth")) {
        System.out.println("Size : " + ((cloth) test).size);
        System.out.println("Age : " + ((cloth) test).age);
    }else if(((String) test.Category).split(",")[1].contentEquals("wear")) {
        System.out.println("Brand : " + ((wear) test).Brand);
    }
}
}

public class ProductController {
private static ProductDB prodDB = new ProductDB();

public static Product getProduct(int prodID) {
    Product product;

    List<Object> prodTemp = prodDB.getProductDetails(prodID);
    String Category[] = ((String) prodTemp.get(6)).split(",");

    switch(Category[1]) {
    case "Cloth":
        product = new cloth(...);
        break;
    case "Wear":
        product = new wear(...);
        break;
    default:
        product = new Default(...);
    }

    return product;
}

public static String getCategory(int prodID) {
    return prodDB.getCategory(prodID).split(",")[1];
}
}

public abstract class Product {

public int ProductNo;
public String Title;
public String Desc;
public String ShortDesc;
public float RegularPrice;
public float SalePrice;
public boolean StockStatus;
public String Category;

public void setRegularPrice(float regularPrice) {
    RegularPrice = regularPrice;
    setSalePrice(regularPrice);
}

protected abstract void setSalePrice(float regularPrice2);

public float getSalePrice() {
    return SalePrice;
}

public void setStockStatus(boolean stockStatus) {
    StockStatus = stockStatus;
}

public boolean isInStock() {
    return StockStatus;
}
public Product(int productNo2, String title, String desc, String shortDesc, float regularPrice, boolean stock, String Category) {
    ProductNo = productNo2;
    Title = title;
    Desc = desc;
    ShortDesc = shortDesc;
    setRegularPrice(regularPrice);
    StockStatus = stock;
    this.Category = Category;
}
public Product(int productNo, String title, String desc, String shortDesc) {
    ProductNo = productNo;
    Title = title;
    Desc = desc;
    ShortDesc = shortDesc;
}
public Product(int productNo, String title, String desc, String shortDesc, float regularPrice) {
    ProductNo = productNo;
    Title = title;
    Desc = desc;
    ShortDesc = shortDesc;
    setRegularPrice(regularPrice);
}
}

public class cloth extends Product{
public String size;
public int age;

public cloth(int productNo, String title, String desc, String shortDesc, float regularPrice, boolean stock, String Category, String size, int age) {
    super(productNo, title, desc, shortDesc, regularPrice, stock, Category);
    this.size = size;
    this.age = age;

}

@Override
protected void setSalePrice(float regularPrice2) {
    SalePrice = (float) (regularPrice2 * 0.85);
}

}

1 个答案:

答案 0 :(得分:8)

你必须在每个开关案例后放置break。如果您测试此代码:

public static void main(String[] args) {

        int test = 2;
        switch (test){
            case 1:
                System.out.println("One");
            case 2:
                System.out.println("Two");
            case 3:
                System.out.println("Three");
            default:
                System.out.println("Default");

        }
}

你会得到这个输出:

Two
Three
Default

所以上面的代码必须像这样改变:

 public static void main(String[] args) {

        int test = 2;
        switch (test){
            case 1:
                System.out.println("One");
                break;
            case 2:
                System.out.println("Two");
                break;
            case 3:
                System.out.println("Three");
                break;
            default:
                System.out.println("Default");

        }
}