java重写抽象方法的正确性

发布于 2024-11-07 11:14:53 字数 728 浏览 4 评论 0原文

鉴于以下情况:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

佞臣 2024-11-14 11:14:54

ContactExtensionProviderLocal 是一个 MutableEntityProvider,因此这意味着只要它继续在系统的其他部分中充当 MutableEntityProvider 即可了解有关 ContactExtensionProviderLocal 的任何信息,您就已经很好地使用了 OO 原则,并且不会遇到任何问题。

ContactExtensionProviderLocal is-a MutableEntityProvider, so that means as long as it continues to function as a MutableEntityProvider in other parts of your system that don't know anything about a ContactExtensionProviderLocal, you've used OO principles well and you won't have any problems.

满身野味 2024-11-14 11:14:54

只要您使用的子类型符合指定的接口,就应该没问题。在这种情况下,它会遵守,因为您正在扩展父接口。

另一方面,如果您的所有特定实现都需要返回特定类型,我会质疑是否需要在您的基(抽象)类中包含此方法。

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.

风启觞 2024-11-14 11:14:54

是的,覆盖返回类型以返回子类是可以的。您仍然履行 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.

舟遥客 2024-11-14 11:14:53

您不能覆盖返回类型。您只能提供(或指定)狭义的实现。这称为协变返回类型(自 Java 5 起)。例如

abstract class A {
    abstract Serializable getId();
}

abstract class B extends A {
    @Override
    abstract Number getId();
}

class C extends B {

    @Override
    Integer getId() {
        return 42;
    }

}

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

abstract class A {
    abstract Serializable getId();
}

abstract class B extends A {
    @Override
    abstract Number getId();
}

class C extends B {

    @Override
    Integer getId() {
        return 42;
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文