开发者

Overriding constructors in Java

开发者 https://www.devze.com 2023-01-12 07:27 出处:网络
For school I need to learn Java and since I\'m used to C++ (like Cocoa/Objective-C) based languages, I get really frustrated on Java.

For school I need to learn Java and since I'm used to C++ (like Cocoa/Objective-C) based languages, I get really frustrated on Java.

I've made a super-class (that can also be used as a base-class):

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() {
     开发者_C百科   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");
    }

}

Now, when I call it like this:

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

I get this:

CellView_constructor

ImageCellView_constructor

CellView_draw

However, I want it to be:

CellView_constructor

ImageCellView_constructor

CellView_draw

ImageCellView_draw

How can I do this?

Thanks in advance,

Tim

EDIT:

I also implemented this method to CellView:

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

And this to ImageCellView:

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

And I've been calling this:

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

Does this causes the problem (when I call a function from the super it calls to the super only)? How can I solve this... (without having to redefine/override the needsRedraw()-method in every subclass?)


You should get proper output.

I tried you example just commented unrelated things:

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");
    }

}

and I get following output:

CellView_constructor

ImageCellView_constructor

CellView_draw

ImageCellView_draw

This is exactly what you want, and this is what your code print's.


The short answer is "you can't."

Objects are constructed from the bottom up, calling base class initializers before subclass initializers and base class consrtuctors before subclass constructors.

EDIT:

The code you have looks good, based on your edit. I would go through the mundane tasks like ensuring that you have compiled your code after you've added you System.out.println calls to your subclass

0

精彩评论

暂无评论...
验证码 换一张
取 消