简体   繁体   English

扩展类构造函数

[英]Extended class constructor

I got a very complex class called Line, and a extended one, ColouredLine that just adds a colour to it. 我有一个非常复杂的类,称为Line,还有一个扩展类,即ColouredLine,它只是为其添加了颜色。

How can I create a ColouredLine from a Line in the constructor ? 如何从构造函数中的Line创建ColouredLine?

Here is my code (that doesn't work…) 这是我的代码(不起作用…)

private class ColouredLine extends Line {
    private Color color;
    public ColouredLine(Line line, Color color) {
        this = (ColouredLine)line;
        this.color = color;
    }

}

Thanks ! 谢谢 !

You would need a copy constructor in the Line class : 您将在Line类中需要一个复制构造函数:

public class Line 
{
    ...
    public Line (Line line)
    {
        // copy properties of line
        this.something = line.something ...
    }
    ...
}

Then in ColouredLine 's constructor, you call Line 's copy constructor : 然后在ColouredLine的构造函数中,调用Line的副本构造函数:

private class ColouredLine extends Line {
    private Color color;
    public ColouredLine(Line line, Color color) {
        super (line);
        this.color = color;
    }

}

Use delegate model. 使用委托模型。 No need to modify the base Line class. 无需修改基本Line类。

private class ColouredLine extends Line {
    private Color color;
    private final Line line;

    public ColouredLine(Line line, Color color) {
        this.line = line;
        this.color = color;
    }

    /**
    * Delegates Line methods to the line object
    **/
    @Override
    public int getProperty1() {
        return line.getProperty1();
        }

    /**
    * Specific ColouredLine methods 
    **/
    public Color getColor() {
        return color;
        }
}

Improper usage of the 'this' keyword: 'this'关键字使用不当:

'this' can only be used in one of two ways: “ this”只能以以下两种方式之一使用:

  1. To avoid confusion when a method of constructor parameter shares the same name as their fields. 为了避免混淆,当构造函数参数的方法与其字段共享相同的名称时。 For example: 例如:

 public void change(int age) { String name = "Tom"; this.age = age; this.name = name; } 

  1. Explicit constructor invocation: From within a constructor, you can also use the this keyword to call another constructor in the same class. 显式构造函数调用:在构造函数内部,您还可以使用this关键字在同一类中调用另一个构造函数。

 public class Rectangle { private int x, y; private int width, height; public Rectangle() { this(0, 0, 1, 1); } public Rectangle(int width, int height) { this(0, 0, width, height); } public Rectangle(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; } ... } 

'this' can never be used by itself, or to invoke a constructor from a super class. 'this'不能单独使用,也不能从超类调用构造函数。

Instead, use the Super() keyword to call the modified super constructor. 而是使用Super()关键字来调用修改后的超级构造函数。 Before doing so, you have to modify the super class constructor to take a Line object in its parameter, like this, 在执行此操作之前,您必须修改超类构造函数,以在其参数中使用Line对象,如下所示,

public Line (Line line)

And in the first line of the sub class constructor, simply call the super constructor: 在子类构造函数的第一行中, 只需调用超级构造函数即可:

super(line);

Note that without defining a new constructor, the default constructor public Line() is always going to be called in all of its subclasses. 请注意,在没有定义新构造函数的情况下,总是会在其所有子类中调用默认构造函数public Line()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM