java重写抽象方法的正确性
鉴于以下情况:
public class RegistryIdModel extends AbstractTableModel{
ContactExtensionProviderLocal provider;
.
.
.
@Override
public ContactRegistryIdProviderLocal getProvider() {
// TODO Auto-generated method stub
return provider;
}
}
public abstract class AbstractTableModel extends AbstractModel{
public abstract MutableEntityProvider getProvider();
}
public interface ContactRegistryIdProviderLocal extends MutableEntityProvider<EppContactRegistryId> {
....some methods....
}
让 要覆盖的RegistryIdModel中的方法 a 的返回类型 ContactRegistryProviderLocal?
如果我这样做,我的生活会更轻松,但以后会产生什么影响?
寻求任何帮助或有用的评论! 马丁
Given the following:
public class RegistryIdModel extends AbstractTableModel{
ContactExtensionProviderLocal provider;
.
.
.
@Override
public ContactRegistryIdProviderLocal getProvider() {
// TODO Auto-generated method stub
return provider;
}
}
public abstract class AbstractTableModel extends AbstractModel{
public abstract MutableEntityProvider getProvider();
}
public interface ContactRegistryIdProviderLocal extends MutableEntityProvider<EppContactRegistryId> {
....some methods....
}
Is it a good/bad solution to let the
method in RegistryIdModel to Override
the return type to a
ContactRegistryProviderLocal?
If I do this, it makes my life easier but what might the effects be later on?
Tanks for any help or helpful comments!
Marthin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
ContactExtensionProviderLocal
是一个MutableEntityProvider
,因此这意味着只要它继续在系统的其他部分中充当MutableEntityProvider
即可了解有关ContactExtensionProviderLocal
的任何信息,您就已经很好地使用了 OO 原则,并且不会遇到任何问题。ContactExtensionProviderLocal
is-aMutableEntityProvider
, so that means as long as it continues to function as aMutableEntityProvider
in other parts of your system that don't know anything about aContactExtensionProviderLocal
, you've used OO principles well and you won't have any problems.只要您使用的子类型符合指定的接口,就应该没问题。在这种情况下,它会遵守,因为您正在扩展父接口。
另一方面,如果您的所有特定实现都需要返回特定类型,我会质疑是否需要在您的基(抽象)类中包含此方法。
As long as the subtype that you're using complies with the specified interface, you should be ok. In this scenario, it would comply since you're extending the parent interface.
On another note, if all your specific implementations need to return specific types, I would question the need to include this method in your base (abstract) class.
是的,覆盖返回类型以返回子类是可以的。您仍然履行
AbstractTableModel
指定的合同。请注意,这仅适用于 Java 1.5 或更高版本。 Java 1.4 不支持协变返回类型。
Yes, overriding the return type to return a subclass is fine. You still fulfill the contract specified by
AbstractTableModel
.Note that this will only work in Java 1.5 or above. Java 1.4 does not support covariant return types.
您不能覆盖返回类型。您只能提供(或指定)狭义的实现。这称为协变返回类型(自 Java 5 起)。例如
You cannot override the return type. You can only provide (or specify) narrow implementation. This is called covariant return types (since Java 5). For example