抽象类返回 [英] Abstract class return
问题描述
对不起,如果这是小事.我创建了一个带有一些子类的抽象类.控制器类创建请求的子类类型的抽象类类型的对象,并返回实现的抽象类.子类具有特定的属性.我无法访问返回对象的那些属性,因为这是抽象类默认类型,所以我尝试进行转换.但这会产生错误item.Default 不能转换为 item.cloth"
Sorry if this is something small. I have created an abstract class with some child classes. A controller class creates a object of type abstract class of requested child class type and returns the abstract class implemented. The child classes have specific attributes for them. I can't access those attributes of the returned object since this is of the abstract class default type, so i tried casting. But this give an error "item.Default cannot be cast to 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 case 之后放置 break
.如果您测试此代码:
You have to put the break
after every switch case. If you test this code:
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
所以上面的代码要改成这样:
So the above code has to be changed like this:
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");
}
}
这篇关于抽象类返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!