【问题标题】:Abstract class return抽象类返回
【发布时间】:2018-04-25 03:36:17
【问题描述】:

对不起,如果这是一件小事。我创建了一个带有一些子类的抽象类。控制器类创建请求的子类类型的抽象类类型的对象并返回实现的抽象类。 子类具有它们的特定属性。我无法访问返回对象的那些属性,因为这是抽象类默认类型,所以我尝试了强制转换。但这会给出错误“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);
}

}

【问题讨论】:

  • 您可以在代码中指出哪一行出现错误?同时发布您的课程
  • 使用 switch 语句和强制转换是误用面向对象的标志。
  • 一些关于Java命名约定的cmets:不要以小写开头的类名,不要以大写开头的局部变量名
  • break语句放在每个switch case之后。
  • Henry,有没有更好的方法来做我想做的铸造

标签: java oop inheritance


【解决方案1】:

你必须在每个 switch case 后面加上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");

        }
}

【讨论】:

  • 似乎问题出在铸造上。有没有正确的方法来做到这一点
  • 对不起,我第一眼就发现了这个(常见的)错误。您发布了很多代码,因此请尝试调试您的程序并查看发生了什么。祝你好运,朋友
  • 哦!感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2010-12-13
  • 2021-11-12
  • 1970-01-01
  • 2011-12-16
  • 1970-01-01
  • 2021-09-23
  • 2013-10-12
  • 1970-01-01
相关资源
最近更新 更多