代码之家  ›  专栏  ›  技术社区  ›  cutsoy

重写Java中的构造函数

  •  0
  • cutsoy  · 技术社区  · 16 年前

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

    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\u构造函数

    但是,我希望它是:

    ImageCellView\u构造函数

    ImageCellView\u绘图

    提前谢谢,

    提姆

    我还在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=新建ImageCellView();
    

    这是否会导致问题(当我从super调用函数时,它会调用super

    2 回复  |  直到 6 年前
        1
  •  2
  •   YoK    16 年前

    你应该得到适当的输出。

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

    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\u构造函数

    ImageCellView\u绘图

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

        2
  •  1
  •   akf    16 年前

    简单的回答是“你不能”

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

    编辑:

    根据你的编辑,你的代码看起来不错。我会完成一些平凡的任务,比如确保在添加代码之后编译代码 System.out.println 调用子类

    推荐文章