Java 无法从继承中找到匹配项
在java中,我有一个问题要问你 我有产品课和电视课。TV类是从产品继承而来的。我还上过商店课 Product类有变量和自己的findMatch方法, TV类有自己的变量和自己的findMatch方法, 存储类具有ArrayList和findProduct方法 在驱动程序类中,我添加了一些产品,创建对象并将它们添加到ArrayList。如果属性在TV类中,则尝试findMatch方法。但是我想找到的属性在Product类(例如brand)中,它找不到。 这些代码有什么问题,我无法解决Java 无法从继承中找到匹配项,java,inheritance,Java,Inheritance,在java中,我有一个问题要问你 我有产品课和电视课。TV类是从产品继承而来的。我还上过商店课 Product类有变量和自己的findMatch方法, TV类有自己的变量和自己的findMatch方法, 存储类具有ArrayList和findProduct方法 在驱动程序类中,我添加了一些产品,创建对象并将它们添加到ArrayList。如果属性在TV类中,则尝试findMatch方法。但是我想找到的属性在Product类(例如brand)中,它找不到。 这些代码有什么问题,我无法解决 publi
public class Product
{
private String barcode;
private String brand;
private String manufactureYear;
private int price;
private int yearOfGuarantee;
private int displaySize;
//constructor and set & get methods here
public boolean findMatch(String keyword)
{
return getBarcode().equals(keyword) ||
getBrand().equals(keyword) ||
getManufactureYear().equals(keyword)
|| Integer.toString(getPrice()).equals(keyword)
|| Integer.toString(getYearOfGuarantee()).equals(keyword)
||Integer.toString(getDisplaySize()).equals(keyword);
}
}
public class TV extends Product
{
private String type;
private String resolution;
//constructor and set&get methods here
public boolean findMatch(String keyword)
{
super.findMatch(keyword);
return getType().equals(keyword) || getResolution().equals(keyword);
}
}
public class Store
{
ArrayList<Product>pList=new ArrayList<>();
public void findProduct(String keyword)
{
for(int i=0; i<pList.size(); i++)
{
if(pList.get(i).findMatch(keyword)==true)
{
System.out.println(pList.get(i));
}
}
}
}
公共类产品
{
私有字符串条形码;
自有品牌;
私人字符串制造商年;
私人int价格;
私人担保机构;
私有整数显示大小;
//构造函数和set&get方法
公共布尔findMatch(字符串关键字)
{
返回getBarcode().equals(关键字)||
getBrand().equals(关键字)||
getManufactureYear().equals(关键字)
||Integer.toString(getPrice()).equals(关键字)
||Integer.toString(getYearOfGuarante()).equals(关键字)
||Integer.toString(getDisplaySize()).equals(关键字);
}
}
公共类电视扩展产品
{
私有字符串类型;
私有字符串解析;
//构造函数和set&get方法
公共布尔findMatch(字符串关键字)
{
super.findMatch(关键字);
返回getType().equals(关键字)| | getResolution().equals(关键字);
}
}
公共类商店
{
ArrayListList=新的ArrayList();
公共无效findProduct(字符串关键字)
{
对于TV
findMatch
方法中的(int i=0;i),您调用该方法的父版本,但不对返回的值执行任何操作:
public boolean findMatch(String keyword)
{
super.findMatch(keyword);
return getType().equals(keyword) || getResolution().equals(keyword);
}
您可能需要以下内容:
public boolean findMatch(String keyword)
{
return super.findMatch(keyword) || getType().equals(keyword) || getResolution().equals(keyword);
}
不仅TV对象,我还有PC和SmarthPhone对象,它们还有findMatch方法。当我尝试使用TV类的属性(类型和分辨率)调用findMatch方法时,它会找到。但它无法从继承类中找到属性。