如何修复CuentaBancaria实现CuentaBancariaInterfaz的MATE编译错误?
问题:Java接口实现编译错误——未覆盖抽象方法setTitular(TitularInterfaz)
我在完成大学银行系统项目时,编写了CuentaBancaria.java类以实现CuentaBancariaInterfaz接口,但使用MATE Terminal编译时出现如下错误:
'CuentaBancaria is not abstract and does not override abstract method setTitular(TitularInterfaz) in CuentaBancariaInterfaz'
此前已修复Makefile中遗漏Saldo.class的问题,但该错误仍未解决,希望找到修复方法。
相关代码
package fp2.poo.pfpooWFG8420; import fp2.poo.utilidades.CuentaBancariaInterfaz; import fp2.poo.utilidades.SaldoInterfaz; import fp2.poo.utilidades.Excepciones.OperacionNoPermitidaExcepcion; import fp2.poo.pfpooWFG8420.Titular; import fp2.poo.utilidades.TelefonoInterfaz; import fp2.poo.utilidades.CorreoElectronicoInterfaz; import fp2.poo.pfpooWFG8420.NumeroDeCuenta; import fp2.poo.pfpooWFG8420.Saldo; /** * Implementacion de la clase CuentaBancaria a partir de una * interfaz * */ public class CuentaBancaria implements CuentaBancariaInterfaz { // Atributos private Saldo saldo; private NumeroDeCuenta numeroDeCuenta; private Titular titular; // Constructor /** * Constructor de CuentaBancaria * * @see CuentaBancaria#CuentaBancaria() */ public CuentaBancaria() { this.saldo = new Saldo(); this.numeroDeCuenta = new NumeroDeCuenta(); this.titular = new Titular(); } // Metodos // Hereda comentarios de documentacion public void setSaldo(Saldo saldo) { this.saldo = saldo; } // Hereda comentarios de documentacion public Saldo getSaldo() { return this.saldo; } // Hereda comentarios de documentacion public void setNumeroDeCuenta(NumeroDeCuenta numeroDeCuenta) { this.numeroDeCuenta = numeroDeCuenta; } // Hereda comentarios de documentacion public NumeroDeCuenta getNumeroDeCuenta() { return this.numeroDeCuenta; } // Hereda comentarios de documentacion public void setTitular(Titular titular) { this.titular = titular; } // Hereda comentarios de documentacion public Titular getTitular() { return this.titular; } }
修复方案
问题核心
CuentaBancariaInterfaz接口定义的setTitular方法参数类型是TitularInterfaz,但你的CuentaBancaria类中实现的setTitular方法参数是自定义的Titular类,两者类型不匹配,Java判定你未正确覆盖接口的抽象方法。
具体修复步骤
确保
Titular类实现TitularInterfaz接口
首先修改Titular类,让它实现对应的接口:package fp2.poo.pfpooWFG8420; import fp2.poo.utilidades.TitularInterfaz; public class Titular implements TitularInterfaz { // 实现TitularInterfaz中要求的所有抽象方法 }修正
CuentaBancaria中的setTitular方法参数类型
将方法的参数类型改为TitularInterfaz,同时可以选择调整成员变量类型以符合面向接口编程的原则:// 方案1:保持成员变量为Titular类型,强制转换参数 public void setTitular(TitularInterfaz titular) { this.titular = (Titular) titular; } // 方案2:将成员变量类型改为TitularInterfaz(更推荐) // private TitularInterfaz titular; // public void setTitular(TitularInterfaz titular) { // this.titular = titular; // }验证所有接口方法的覆盖完整性
检查CuentaBancariaInterfaz中的所有抽象方法,确保CuentaBancaria类中都有完全匹配(参数类型、返回值类型、方法名一致)的实现。
内容的提问来源于stack exchange,提问作者RODSIN
相关产品推荐
相关产品推荐

