clone()具有受保护的访问权限-公开对象clone() [英] clone() has protected access - made public Object clone()

查看:173
本文介绍了clone()具有受保护的访问权限-公开对象clone()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写代码以创建一个对象,克隆该对象,然后将两者进行比较。

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

有问题的对象Octagon是对象的扩展GeometricObject

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

public class Octagon extends GeometricObject implements Comparable<Octagon>, Cloneable {
private double side;

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

public Object clone() throws CloneNotSupportedException {
    Octagon octClone = (Octagon)super.clone();
    return octClone;
}

在名为Octagon.java的文件中

In a file named Octagon.java

我的主要方法是TestOctagon.java。

In another, TestOctagon.java, is my main method:

public class TestOctagon {
    public static void main(String[] args) {
        GeometricObject test = new Octagon(5); //create an Octagon with a side of 5
        System.out.println("Area is: "+test.getArea());
        System.out.println("Perimeter is: "+test.getPerimeter());

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


    }
}

错误来了

clone() has protected access in Object

我尝试重命名Octagaon中的clone方法,对clome说,但是然后我得到了错误:

I've tried renaming the clone method in Octagaon, say to cloneme, but then I get the error:

cannot find symbol
symbol: method cloneme()
location: variable test of type GeometricObject

我感觉到问题是因为八边形扩展了另一个对象,也许是这样吗?

I get the feeling the problem is because Octagon extends another object, maybe?

我真的找不到任何解决方案,我花了一个小时阅读这里的所有其他clone()帖子。

I really can't find any solution, and I've spent a good hour reading all the other clone() posts here.

编辑:这是我使用克隆所必需的。我知道一般的共识是克隆很无聊。

It's required I use clone. I'm aware the general consensus is clone is borked.

推荐答案

替换

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

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

,以便被调用的克隆方法是您的类之一。

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

这篇关于clone()具有受保护的访问权限-公开对象clone()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆