Java 中的部分重写(或重载时的动态重写)

发布于 2024-08-29 18:52:01 字数 2535 浏览 1 评论 0原文

如果我有一个父子定义了一些方法 .foo() 像这样:

class Parent {
  public void foo(Parent arg) {
    System.out.println("foo in Function");
  }
}
class Child extends Parent {
  public void foo(Child arg) {
    System.out.println("foo in ChildFunction");
  }  
}

当我这样调用它们时:

  Child f = new Child();
  Parent g = f;
  f.foo(new Parent());
  f.foo(new Child());  
  g.foo(new Parent());
  g.foo(new Child());

输出是:

foo in Parent
foo in Child
foo in Parent
foo in Parent

但是,我想要这个输出:

foo in Parent
foo in Child
foo in Parent
foo in Child

我有一个扩展父类的子类。在 Child 类中,我想“部分重写”Parent 的 foo(),也就是说,如果参数 arg 的类型是 Child,则 Child 的 foo () 被调用,而不是 Parent 的 foo()

当我作为孩子调用 f.foo(...) 时,效果很好;但如果我像 g.foo(...) 中那样从其父别名引用它,那么无论 foo(..) 的类型如何,都会调用父级的 foo(..)代码>arg。

据我了解,我所期望的事情不会发生,因为Java中的方法重载是早期绑定(即在编译时静态解析),而方法重写是后期绑定(即在编译时动态解析)并且因为我定义了一个函数使用技术上不同的参数类型,我在技术上用不同的定义重载父级的类定义,而不是覆盖它。但我想做的是,当 .foo() 的参数是父级 foo() 参数的子类时,概念上“部分重写”。

我知道我可以在 Child 中定义一个存储桶覆盖 foo(Parent arg) 来检查 arg 的实际类型是 Parent 还是 Child 并正确传递它,但是如果我有 20 个 Child,那将是大量重复类型不安全的代码。

在我的实际代码中,Parent 是一个名为“Function”的抽象类,它只是抛出 NotImplementedException()。子项包括“多项式”、“对数”等,.foo() 包括 Child.add(Child)、Child.intersectionsWith(Child) 等。并非所有 Child.foo(OtherChild) 的组合都是可解的并且在事实上,甚至不是所有的 Child.foo(Child) 都是可以解决的。因此,我最好定义所有未定义的内容(即抛出 NotImplementedException),然后仅定义那些可以定义的内容。

所以问题是:有没有办法只覆盖父级的 foo() 的一部分?或者有更好的方法来做我想做的事吗?

编辑

@Zeiss:如果我使用双重调度,如下所示:

class Parent {
  public void foo(Parent arg) {
    System.out.println("foo in Parent");
  }
}
class Child extends Parent {
  public void foo(Parent arg) {
    System.out.println("foo in Child(Parent)");
    arg.foo(this);
  }
  public void foo(Child arg) {
    System.out.println("foo in Child(Child)");
  }
}

我得到无限递归:

(stack): 
StackOverflowError: ...
...
    at sketch_apr25a$Child.foo(sketch_apr25a.java:35)
...
(output):
...
foo in Child(Parent)
...

执行g.foo(new Child());时。其余的似乎都很好,因为输出是:

foo in Child(Parent)
foo in Parent

foo in Child(Child)

foo in Child(Parent)
foo in Parent

foo in Child(Parent)
(infinite recursion follows)

为什么会发生这种情况? g 是父级的别名,但它正在访问子级的 foo(Parent)?

If I have a parent-child that defines some method .foo() like this:

class Parent {
  public void foo(Parent arg) {
    System.out.println("foo in Function");
  }
}
class Child extends Parent {
  public void foo(Child arg) {
    System.out.println("foo in ChildFunction");
  }  
}

When I called them like this:

  Child f = new Child();
  Parent g = f;
  f.foo(new Parent());
  f.foo(new Child());  
  g.foo(new Parent());
  g.foo(new Child());

the output is:

foo in Parent
foo in Child
foo in Parent
foo in Parent

But, I want this output:

foo in Parent
foo in Child
foo in Parent
foo in Child

I have a Child class that extends Parent class. In the Child class, I want to "partially override" the Parent's foo(), that is, if the argument arg's type is Child then Child's foo() is called instead of Parent's foo().

That works Ok when I called f.foo(...) as a Child; but if I refer to it from its Parent alias like in g.foo(...) then the Parent's foo(..) get called irrespective of the type of arg.

As I understand it, what I'm expecting doesn't happen because method overloading in Java is early binding (i.e. resolved statically at compile time) while method overriding is late binding (i.e. resolved dynamically at compile time) and since I defined a function with a technically different argument type, I'm technically overloading the Parent's class definition with a distinct definition, not overriding it. But what I want to do is conceptually "partially overriding" when .foo()'s argument is a subclass of the parent's foo()'s argument.

I know I can define a bucket override foo(Parent arg) in Child that checks whether arg's actual type is Parent or Child and pass it properly, but if I have twenty Child, that would be lots of duplication of type-unsafe code.

In my actual code, Parent is an abstract class named "Function" that simply throws NotImplementedException(). The children includes "Polynomial", "Logarithmic", etc and .foo() includes things like Child.add(Child), Child.intersectionsWith(Child), etc. Not all combination of Child.foo(OtherChild) are solvable and in fact not even all Child.foo(Child) is solvable. So I'm best left with defining everything undefined (i.e. throwing NotImplementedException) then defines only those that can be defined.

So the question is: Is there any way to override only part the parent's foo()? Or is there a better way to do what I want to do?

EDIT:

@Zeiss: If I use Double Dispatch, like this:

class Parent {
  public void foo(Parent arg) {
    System.out.println("foo in Parent");
  }
}
class Child extends Parent {
  public void foo(Parent arg) {
    System.out.println("foo in Child(Parent)");
    arg.foo(this);
  }
  public void foo(Child arg) {
    System.out.println("foo in Child(Child)");
  }
}

I got infinite recursion:

(stack): 
StackOverflowError: ...
...
    at sketch_apr25a$Child.foo(sketch_apr25a.java:35)
...
(output):
...
foo in Child(Parent)
...

when executing g.foo(new Child());. The rest seems to be fine, as the output is:

foo in Child(Parent)
foo in Parent

foo in Child(Child)

foo in Child(Parent)
foo in Parent

foo in Child(Parent)
(infinite recursion follows)

Why do this happen? g is the Parent's alias, but it's accessing Child's foo(Parent)?

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

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

发布评论

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

评论(2

寻找一个思念的角度 2024-09-05 18:52:01

这不是 双重调度 的用例吗?


更新:

class Function {
    public void add(Function f) {
        f.addMe(this);
    }

    public void addMe(Function f) {
        // Default case
        throw NotImplementedException();
    }
    public void addMe(Logarithmic log) {
        // Code that handles the real
    }
}

class Logarithmic extends Function {
    // This object supports adding
    public void add(Function f) {
        f.addMe(this);
    }
}


Logarithmic log = new Logarithmic();
log.add(new Function()); 
log.add(new Logarithmic()); 

Function f = log;
f.add(new Function()); 
f.add(new Logarithmic()); 

Isn't this a use case for Double Dispatching ?


Update:

class Function {
    public void add(Function f) {
        f.addMe(this);
    }

    public void addMe(Function f) {
        // Default case
        throw NotImplementedException();
    }
    public void addMe(Logarithmic log) {
        // Code that handles the real
    }
}

class Logarithmic extends Function {
    // This object supports adding
    public void add(Function f) {
        f.addMe(this);
    }
}


Logarithmic log = new Logarithmic();
log.add(new Function()); 
log.add(new Logarithmic()); 

Function f = log;
f.add(new Function()); 
f.add(new Logarithmic()); 
忆离笙 2024-09-05 18:52:01

我通过像这样显式覆盖 Child 中的 foo(Parent arg) 来让它工作 -

class Parent {
    public void foo(Parent arg) {
        System.out.println("foo in Parent");
    }
}

class Child extends Parent {
    @Override
    public void foo(Parent arg) {
        System.out.println("foo in Child(Parent)");
        if (arg instanceof Child) {
            foo((Child)arg);
        } else {
            super.foo(arg);
        }
    }
    public void foo(Child arg) {
        System.out.println("foo in Child(Child)");
    }
}

这似乎符合以下逻辑

我想“部分重写”Parent 的 foo(),也就是说,如果参数 arg 的类型是 Child,则调用 Child 的 foo() 而不是 Parent 的 foo()。

但您实际上必须重写该方法,而不是“部分重写”该方法。

I got it to work by explicitly overriding foo(Parent arg) in Child like this -

class Parent {
    public void foo(Parent arg) {
        System.out.println("foo in Parent");
    }
}

class Child extends Parent {
    @Override
    public void foo(Parent arg) {
        System.out.println("foo in Child(Parent)");
        if (arg instanceof Child) {
            foo((Child)arg);
        } else {
            super.foo(arg);
        }
    }
    public void foo(Child arg) {
        System.out.println("foo in Child(Child)");
    }
}

This seems to match the logic of

I want to "partially override" the Parent's foo(), that is, if the argument arg's type is Child then Child's foo() is called instead of Parent's foo().

But rather than "partially override" the method, you actually have to override the method.

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