Java - 对于给定函数 foo(X i);在接口X中,为什么不能实现类Y将其更改为foo(Y i)?

发布于 2024-08-26 23:52:40 字数 311 浏览 5 评论 0原文

例如

public interface X{
    public void foo(X i);
}

public class Y implements X{//error: doesn't implement foo(X i)...
    public void foo(Y i){
        fooBar(foo);
    }
    ....
}

为什么我不能这样做?我该如何更改它才能实现这一点? 我该怎么做才能在 X 中使用参数声明 foo,然后能够使用 Y 作为 Y 中的参数类型?

For example

public interface X{
    public void foo(X i);
}

public class Y implements X{//error: doesn't implement foo(X i)...
    public void foo(Y i){
        fooBar(foo);
    }
    ....
}

Why can't I do that? And how can I change it so this is possible? What can I do to declare foo in X with a parameter, and then be able to use Y as the parameter type in Y?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

栀梦 2024-09-02 23:52:40

除了 Don Boyle 所说的之外,如果不向编译器暗示其意图,您就无法更改它。您可以通过向接口引入泛型来实现此目的,如下所示:

public interface X<T> {
    public void foo(T i);
}

public class Y implements X<Y> {
    public void foo(Y i){
        fooBar(foo);
    }
}

Additionally to what Don Boyle said, you can't change it without hinting the compiler of the intention. You do this by introducing Generics to the interface, like so:

public interface X<T> {
    public void foo(T i);
}

public class Y implements X<Y> {
    public void foo(Y i){
        fooBar(foo);
    }
}
粉红×色少女 2024-09-02 23:52:40

通过更改类 Y 中输入参数的类型,您更改了方法的签名,这意味着编译器将其视为完全不同的方法。

Java 接口就像一个契约。任何实现它的东西都必须实现它定义的确切方法。通过使用不同的方法签名,您并没有真正实现定义的方法,因此您违反了该合同。

By changing the type of the input parameter in class Y, you have changed the signature of the method which means the compiler sees it as a completely different method.

A Java interface is like a contract. Anything implementing it must implement the exact methods it defines. By using a different method signature you are not really implementing the defined method and so you are breaking that contract.

呆° 2024-09-02 23:52:40

尝试类似的东西

interface X<T extends X> {
    public void foo(T a);
}

class Y implements X<Y> {
    public void foo(Y a);
}

Try something like

interface X<T extends X> {
    public void foo(T a);
}

class Y implements X<Y> {
    public void foo(Y a);
}
冧九 2024-09-02 23:52:40

假设您已经按照自己的意愿进行了操作,并且 Java 允许这样做。假设另一个类(称为 Z)也实现了 X。因为 Z 实现了 X,并且由于 X 的定义,您必须能够为任何 Z z 调用 X.foo(z)。但是 Y(它是一个 X)不知道如果将 Z 传递给它的 foo() 该怎么办。这就是原因。

Suppose you had done as you want, and suppose Java allowed it. And let's say another class - call it Z - also implements X. Because Z implements X, and because of the definition of X, you must be able to call X.foo(z) for any Z z. But Y, which is an X, doesn't know what to do if you pass a Z to its foo(). That's why.

云朵有点甜 2024-09-02 23:52:40

通过实现接口 X,您承诺实现该接口上的所有方法,这意味着您的 foo 方法可以采用任意 X。
现在,如果您只接受 Ys 作为 foo 方法的参数,则您将无法完全实现该接口,因为实现 X 的所有其他类都不是 foo 的有效参数。

By implementing the interface X, you promise that you implement all methods on this interface, which means that your foo method can take an arbitrary X.
Now if you would just accept Ys as the parameter to your foo method, you would not fully implement the interface as all other classes implementing X were not valid parameters for foo.

爱人如己 2024-09-02 23:52:40

因为接口指定了所有实现类的共同行为。假设你有一些其他类都实现了 X,你会期望如果你有类 X 的对象,你可以使用类 X 的参数调用 foo (可以是它的任何子类或实现),所以假设你' d 有这样的代码:

class Z implements X {
  ...
}

Z z = new Z();
X x = new Y();
x.foo(z);

这是错误的,因为使用您的代码 foo 只会接受 Y 类的参数

Because Interface specifies common behavior for all implementing classes. Let's say you'd have some other classes all implementing X you would expect that if you have object of class X you can call foo with parameter that is of class X (which can be any of it's subclasses or implementations) so let's say you'd have code like this:

class Z implements X {
  ...
}

Z z = new Z();
X x = new Y();
x.foo(z);

Which would be false since with your code foo would only accept arguments of class Y

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