【问题标题】:Overriding constructors in Java在 Java 中重写构造函数
【发布时间】:2010-08-21 10:34:12
【问题描述】:

在学校我需要学习 Java,由于我习惯了基于 C++(如 Cocoa/Objective-C)的语言,我对 Java 感到非常沮丧。

我已经创建了一个超类(也可以用作基类):

public class CellView {
    public CellViewHelper helper; // CellViewHelper is just an example

    public CellView() {
        this.helper = new CellViewHelper();
        this.helper.someVariable = <anything>;

        System.out.println("CellView_constructor");
    }
    
    public void draw() {
        System.out.println("CellView_draw");
    }

    public void needsRedraw() {
        this.draw();
    }

}

public class ImageCellView extends CellView {

    public Image someImage;

    public ImageCellView() {
        super();
        this.someImage = new Image();
        System.out.println("ImageCellView_constructor");
    }

    public void setSomeParam() {
        this.needsRedraw(); // cannot be replaced by this.draw(); since it's some more complicated.
    }

    @Override public void draw() {
        super.draw();
        System.out.println("ImageCellView_draw");
    }

}

现在,当我这样称呼它时:

ImageCellView imageCellView = new ImageCellView();
imageCellView.setSomeParam();

我明白了:

CellView_constructor

ImageCellView_constructor

CellView_draw

但是,我希望它是:

CellView_constructor

ImageCellView_constructor

CellView_draw

ImageCellView_draw

我该怎么做?

提前致谢,

提姆

编辑:

我也在CellView中实现了这个方法:

public void needsRedraw() {
    this.draw();
}

这个到 ImageCellView:

public void setSomeParam() {
    this.needsRedraw(); // cannot be replaced by this.draw(); since it's some more complicated.
}

我一直这样称呼它:

ImageCellView imageCellView = new ImageCellView();
imageCellView.setSomeParam();

这是否会导致问题(当我从 super 调用函数时,它只调用 super )?我该如何解决这个问题...(无需重新定义/覆盖每个子类中的 needsRedraw() 方法?)

【问题讨论】:

  • 你的第一个输出正确吗?好像不是
  • 报告的内容有问题。
  • 这也是 Java 与 C++ 没有区别的一个领域。
  • 您的代码已经运行。你应该澄清你的问题。
  • @Colin,我更新了我的代码,以向您展示代码所做的更多调用......这非常复杂(整个代码甚至更多:P)

标签: java inheritance constructor overriding


【解决方案1】:

你应该得到正确的输出。

我试过你的例子只是评论了不相关的东西:

import java.awt.Image;

public class CellView {
    //public CellViewHelper helper; // CellViewHelper is just an example

    public CellView() {
        //this.helper = new CellViewHelper();
        //this.helper.someVariable = <anything>;

        System.out.println("CellView_constructor");
    }

    public void draw() {
        System.out.println("CellView_draw");
    }

    public static void main(String[] args) {
        ImageCellView imageCellView = new ImageCellView();
        imageCellView.draw();
    }
}

class ImageCellView extends CellView {

    public Image someImage;

    public ImageCellView() {
        super();
        //this.someImage = new Image();
        System.out.println("ImageCellView_constructor");
    }

    @Override public void draw() {
        super.draw();
        System.out.println("ImageCellView_draw");
    }

}

我得到以下输出:

CellView_constructor

ImageCellView_constructor

CellView_draw

ImageCellView_draw

这正是你想要的,这就是你的代码打印出来的。

【讨论】:

  • 哦,对不起,我把输出的顺序放错了……我的意思是 ImageCellView_draw 没有被调用,我不明白为什么……
  • 它仍然没有被调用(输出中没有“ImageCellView_draw”)。我更新了我的代码以向您展示更多方法。
  • 这很奇怪,这行得通,但是相同的代码(具有其他类名和一些对 awt.Graphics 的导入)不起作用...看起来我的图形实现有问题。无论如何,谢谢!
  • 为什么我在这个问题上没有得到任何分数,尽管这个问题已经被评价和选择了?
  • @Yok 你随机决定删除大部分问题并恢复它们,这将你推到了
【解决方案2】:

简短的回答是“你不能”。

对象是自下而上构造的,在子类构造器之前调用基类构造器,在子类构造器之前调用基类构造器。

编辑:

根据您的编辑,您的代码看起来不错。在将 System.out.println 调用添加到子类后,我将完成一些日常任务,例如确保您已编译代码

【讨论】:

  • 哦,对不起,我把输出的顺序放错了……我的意思是 ImageCellView_draw 没有被调用,我不明白为什么……
  • 我更新了我的代码,向您展示了更多方法(包括可能导致问题的超级调用?)。
猜你喜欢
  • 2020-12-02
  • 1970-01-01
  • 1970-01-01
  • 2012-03-06
  • 2010-09-25
  • 2021-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多