clone() has protected access - made public Object clone()

后端 未结 2 490
醉话见心
醉话见心 2020-12-17 09:49

I\'m writing code to create an object, clone the object, then compare the two.

The object in question, Octagon, is an extension of an object GeometricObject

相关标签:
2条回答
  • 2020-12-17 10:25

    You may write a copy-constructor:

    public Octagon( Octagon right ){
        this.side = right.side;
    }
    

    And use it from the clone method:

    public Object clone() throws CloneNotSupportedException {
        return new Octagon( this );
    }
    
    0 讨论(0)
  • 2020-12-17 10:47

    Replace

    Octagon copy = (Octagon)test.clone();
    

    with

    Octagon copy = (Octagon)((Octagon)test).clone();
    

    so that the called clone method is the one of your class.

    0 讨论(0)
提交回复
热议问题