未调用子类中的java重写方法
我有一个子类,它有一个方法,进程覆盖父类中的方法,但它调用的是父类中的方法,而不是子类中的方法 父类未调用子类中的java重写方法,java,overriding,Java,Overriding,我有一个子类,它有一个方法,进程覆盖父类中的方法,但它调用的是父类中的方法,而不是子类中的方法 父类 public class Records { protected String[] process(String table, Integer records, String field) throws Exception { System.out.println("***************process- original"); } public voi
public class Records {
protected String[] process(String table, Integer records, String field) throws Exception {
System.out.println("***************process- original");
}
public void insertRecords {
Records r = new Records()
String[] records = r.process(table, records, field);
String record = records[0];
/* method implementation */
}
}
子类
public class RecordsCustomer extends Records{
@Override
protected String[] process(String table, Integer records, String field) throws Exception {
System.out.println("***************process- subclass");
}
它打印出“******进程-原始”而不是“******进程-子类”。我遗漏了一些东西,但我在代码中看不到它。您的
RecordsCustomer
类没有对Record
类进行子类化
public class RecordsCustomer extends Records {
protected String[] process(String table, Integer records, String field) throws Exception {
System.out.println("***************process- subclass");
}
}
这样说吧,它应该能正常工作
Records records = new RecordsCustomer();
records.process("table", 1, "data");
确保在创建对象和调用方法时:
Records records = new RecordsCustomer();
String[] s = records.process(....);
而不是:
Records records = new Records();
String[] s = records.process(....);
如果按如下所示调用(即,实际对象需要是子类),那么它应该可以工作:
Records records = new RecordsCustomer();
records.process("tableName", 10, "customerName");
注意:为了安全起见,请在测试前进行干净的构建。以以下方式创建对象:
RecordsCustomer myObjectName = new RecordsCustomer();
或
在您的代码中,您的方法声明它们返回一个字符串数组,但该方法本身不返回任何内容,您应该在发布的代码中返回一个字符串数组或将de declaration更改为
void
noextends
(因此没有子类)?请参见附加一些代码如何调用方法如何RecordsCustomer
是记录的子类
?不要键入。复制粘贴。请在调用方法的地方添加一段代码。您仍然没有复制/粘贴代码。显示的代码不会编译(请查看不是方法的insertRecords
方法)。而且您永远不会实例化一个RecordsCustomer
,也不会对其调用process()
。我想提供的答案中有一个能解决你的问题,但我还不确定你真正的问题是什么。我没有抓住这一点。谢谢
Records myObjectName = new RecordsCustomer();