我的子类从超类中传递值

时间:2014-11-30 04:54:39

标签: java inheritance parameters superclass

我正在编写一个java项目,但我遇到了麻烦。我在我的Horizo​​ntal类(子类)中传递新值(0,0)和(6,0),但是当我运行程序时,我的输出是(0,0)和(4,4)这些是传入的值在我的超级班!我不知道如何解决这个问题!如果你让我知道我做错了什么,我将不胜感激。我需要按照说明进行操作!

public class Segment implements SegmentInterface {
    // two Points that hold endpoints of the segment
    private Point p1;
    private Point p2;

    // Default constructor that will set the endpoints to new
    // Points with values (0,0) and (4,4)
    public Segment() {
        //this(0, 0, 4, 4);
        this.p1 = new Point(0, 0);
        this.p2 = new Point(4, 4);
    }

    // Parameterized constructor that accepts (int x1, int y1, int x2, int y2)
    // and creates and sets the endpoints
    public Segment(int x1, int y1, int x2, int y2) {

        //this.p1 = new Point(x1, y1);
        //this.p2 = new Point(x2, y2);

        if(x1 == x2 && y1 == y2){
            throw new IllegalArgumentException("Points can't have a lenght of 0!");
        } 

        else{
            this.p1 = new Point(x1, y1);
            this.p2 = new Point(x2, y2);
    }
}

     //A parameterized constructor that accepts (Point p1, Point p2) and sets both
     //the endpoints to a deep copy of the Points that are passed in.
     //Make sure to check to see if both Points are equal.  This would be a
     //theoretical "Segment" of length 0 which is not allowed, so throw a new
     //IllegalArgumentException.
    public Segment(Point p1, Point p2) {

        this.p1 = new Point(p1.getX(), p1.getY());
        this.p2 = new Point(p2.getX(), p2.getY());

            if(p1.equals(p2)){
                throw new IllegalArgumentException("Points cannot have same values!");
            } 
}

    // Copy constructor that accepts a Segment and initializes the data (of the
    // new Segment being created) to be the same as the Segment that was
    // received.
    public Segment(Segment other) {

        this(other.p1, other.p2);

        //if(other.p1 == other.p2){
            //throw new IllegalArgumentException("Undefine!");
        //}

,这是我的子类:

//HorizontalSegment.java
public class HorizontalSegment extends Segment implements
        HorizontalSegmentInterface {

    protected Point p1;
    protected Point p2;

    //A default constructor that will set the endpoints to new
    //Points with values (0,0) and (6,0)
    public HorizontalSegment() {
        this.p1 = new Point(0, 0);
        this.p2 = new Point(6, 0);
    }

    /*A parameterized constructor that accepts (int x1, int y1, int x2, int y2)
     *      and creates and sets the endpoints.  But first, check to see if x1 == x2
     *      and y1== y2.  This would be a theoretical "Segment" of length 0 which
     *      is not allowed, so throw a new IllegalArgumentException like this:
     *          throw new IllegalArgumentException("whatever explanatory String you want");
     *      Also, check to see if y1 != y2.  This would be a line segment that is not
            horizontal, so throw a new IllegalArgumentException if this occurs.*/
    public HorizontalSegment(int x1, int y1, int x2, int y2) {
        super(x1, y1, x2, y2);
        if (x1 == x2 && y1 == y2) {
            throw new IllegalArgumentException("Segment of length 0 is not allowed!");
        }

        else if (y1 != y2) {
            throw new IllegalArgumentException("Line segment that is not horizontal!");
        } 

        else {
            this.p1 = new Point(x1, y1);
            this.p2 = new Point(x2, y2);
        }
    }

    /*A parameterized constructor that accepts (Point p1, Point p2) and sets both
     *      the endpoints to a deep copy of the Points that are passed in.
     *      Make sure to check to see if both Points are equal.  This would be a
     *      theoretical "Segment" of length 0 which is not allowed, so throw a new
     *      IllegalArgumentException.
     *      Also, check to see if the points form a horizontal segment.  If not, throw a
     *      new IllegalArgumentException if this occurs.*/
    public HorizontalSegment(Point p1, Point p2) {
        if (this.p1.getX() == p2.getX() && this.p1.getY() == p2.getY()) {
            throw new IllegalArgumentException(
                    "Segment of length 0 is not allowed!");

        } else if (this.p1.getY() != p2.getY()) {
            throw new IllegalArgumentException(
                    "Line segment that is not horizontal!");
        } 


        else {
            this.p1 = new Point(p1.getX(), p1.getY());
            this.p2 = new Point(p2.getX(), p2.getY());
        }
    }

    public HorizontalSegment(HorizontalSegment hs) {
        //this.p1 = hs.getP1();
        //this.p2 = hs.getP2();
        this(hs.p1, hs.p2);
    }
}

1 个答案:

答案 0 :(得分:0)

p1类中的p2protected点字段应为Segment(不是private,因为您的子类无法访问它们)。因此,您应该从HorizontalSegment中删除它们,因为它们会添加两个Point的新实例,其名称与Segment中的实例相同(但它们不会覆盖这些值,它们是不同的) 。

在进行这两项更改后,您的代码应该按预期工作。

public class HorizontalSegment extends Segment implements
        HorizontalSegmentInterface {
    // protected Point p1;
    // protected Point p2;

public class Segment implements SegmentInterface {
    // two Points that hold endpoints of the segment
    protected Point p1; // <-- not private
    protected Point p2;