Java多态性的行为与我预期的不一致

huangapple go评论65阅读模式
英文:

Java polymorphism not behaving the way I assumed it would

问题

我有一些困惑,不太明白为什么我的父类没有使用子类中可用的具有相同签名的方法。

代码有点长,所以我只会展示我不理解的代码片段。

父类:

public GameOutcome endGame(GameState game) {
    GameOutcome outcome = calculateOutcome(game);    
    numberOfGames += 1;
    switch(outcome) {

而被 endGame 调用的方法是:

private GameOutcome calculateOutcome(GameState game) {
    System.out.println("错误的方法被调用");
    switch(game) {
        case XWIN:
            return myMove == CellValue.X ? GameOutcome.WIN : GameOutcome.LOSE;

子类:

private GameOutcome calculateOutcome(GameState game) {
    int tracker;

    switch(game) {
        case XWIN:

我的理解是,父类在使用之前会检查子类是否存在 calculateOutcome,然后再使用父类中的方法。

这段代码可以编译并运行,但它没有使用我想要的方法(子类中的方法)。

我唯一能看到可能引起这个问题的地方是,父类已经在使用其中一个函数来调用另一个函数,所以它不会去检查子类?

英文:

I'm having a bit of trouble understanding why my parent class isn't using the method of same signature available in the child class.

The code is a bit long, so I'll just show snippets of my code that I'm not understanding.

Parent class:

public GameOutcome endGame(GameState game) {
GameOutcome outcome = calculateOutcome(game);    
numberOfGames += 1;
switch(outcome) {

and the method that endGame is calling:

private GameOutcome calculateOutcome(GameState game) {
System.out.println("Wrong method being used");
switch(game) {
case XWIN:
  return myMove == CellValue.X ? GameOutcome.WIN : GameOutcome.LOSE;

Child:

private GameOutcome calculateOutcome(GameState game) {
int tracker;

switch(game) {
case XWIN:

My understanding is that the parent class will check if calculateOutcome exists in the child, before using the one in parent.

This code compiles and runs, but it isn't using the method that I want. (The one in the child)

The only thing I see that could be causing this problem is that the parent class is already using one of its functions to call another function, so it won't bother checking child?

答案1

得分: 0

抱歉没有早些时候发布这个。

基本上,我必须仅修改子项,而不触及父项。

我遇到的问题:

  1. 子项试图覆盖私有方法,所以它总是被忽略。

  2. 可以解决此问题的公共父方法必须修改多个私有方法,我不确定如何从子项触及它们。

我通过覆盖公共父方法并使用 super() 解决了这个问题,这样父项可以修改它自己的变量。这使我能够保持私有方法作为我必须覆盖的内容。

对于我的初始问题表达不清,我也感到抱歉。

英文:

Apologize for not posting this earlier.

Basically I had to modify ONLY the child, without touching the parent.

The issues that I had:

1)The child was trying to override a private method, so it was always ignored.

2)The Public parent method that could fix this had to modify several private methods, which I wasn't sure how to touch from the child.

I solved it by overriding the Public Parent method, and then using super() so that the Parent modifies its own variables. This allowed me to keep the Private method I had to override as Private.

I also apologize for my initial question not being clear enough.

huangapple
  • 本文由 发表于 2020年7月25日 08:11:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63082962.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定