Java – inheritance relationship between square / rectangle (with constraint invariants) sharing quadrilateral cardinality

I have an interface polygon, and then I have a quadrilateral Then, I have two classes, square and rectangle, which extend quadrilateral

The quadrilateral consists of instance variables sidea, sideb, sidec and sided It contains the methods area () and perimeter () to calculate the area and perimeter of any quadrilateral

With this in mind, the class square has an instance variable lengthofsides, while the rectangular class has two instance variables length and width

Because affirmative quadrilateral method and Zhou Changke are used to calculate the area and circumference of any quadrilateral, including square and rectangle, I think it is best to construct only a square or rectangle, then call superclass to specify the affirmative edge (quadrilateral area and circumference calculation). In addition, when changing the instance variable in square or rectangle, the setter also updates the associated value in the parent class

This is the square class:

/**
 * A model for a Square.
 * 
 * @author BTKS
 */
public class Square extends Quadrilateral {

    private static final double ANGLES_SUM = 180; // the total sum of two opposite angles in degrees

    private double lengthOfSides; // the length of each side

    /**
     * Construct a new Square.
     * 
     * @param lengthOfSides the length of each side
     */
    public Square(double lengthOfSides) {
        super(ANGLES_SUM,lengthOfSides,lengthOfSides);

        this.lengthOfSides = lengthOfSides;
    }

    /**
     * @return the length of each side
     */
    public double getLengthOfSides() {
        return lengthOfSides;
    }

    /**
     * @param lengthOfSides the length of each side
     */
    public void setLengthOfSides(double lengthOfSides) {
        this.lengthOfSides = lengthOfSides;

        super.setSideA(lengthOfSides);
        super.setSideB(lengthOfSides);
        super.setSideC(lengthOfSides);
        super.setSideD(lengthOfSides);
    }
}

Is this considered bad practice? It was for a university mission, and she didn't specify what she was looking for If I don't use anything in quads in quad, extending quads is like useless

Solution

This depends on what you want the inheritance relationship to represent here Generally, what people want to express is better not to use the so-called "Object-Oriented inheritance" (in fact, I find that this inheritance is rarely used)

In this case, the inheritance relationship seems to express the fact that subclasses have "additional constraints" relative to superclasses If polygon itself is a class rather than an interface:

>Polygon (anything, as long as it is convex) > quadrilateral (additional constraint with only 4 sides) > rectangle (additional constraint with PI / 2 angle) > square (additional constraint with the same side length)

Basically, I will:

Or just write quadrilateral, delete setter and make all instance variables final If someone changes the edge alone, you can get rid of the uncontrollable change in square Generally speaking, the finals is a good idea Subclasses are then reduced to only special constructors

Or just write quadrilateral without subclasses, and give it Boolean check: isrectangle(), issquare(), maybe leave setter But it doesn't sound so elegant

If you can find it, you can also recommend Bertrand Meyer's "the many faces of inheritance: a taxonomy of taxonomy" (1996). There is a payment line, but someone nearby may use IEEE Xplore to access it

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>