circled.java:14: unreported exception java.lang.CloneNotSupportedException; mustbe caught or declared to be thrown
mypoint p2=(mypoint)p1 .clone();
请问这是什么地方错了,
求高手帮忙了,
----------------解决方案--------------------------------------------------------
也就是说,这条语句还需要一个try catch, 或者在函数旁注明 throws ...
你自己不会的话,把代码贴出来,我帮你改改。
----------------解决方案--------------------------------------------------------
你说得很对,我加了try catch后,没再出现这个错误,但又出现了另一个错误提示, mycircle.java:23: missing return statement } 我的源代码如下: circled.java import javax.swing.JOptionPane; import java.lang.CloneNotSupportedException; public class circled { private static String heads ="this is a cirle"; public static void main(String argv[]) { try { System.out.println(heads); String inp; int r; inp=JOptionPane.showInputDialog("input radius:"); r=Integer.parseInt(inp); mypoint p1=new mypoint(10,10); mypoint p2=(mypoint)p1.clone(); System.out.println("center at :"+p1); System.out.println("center at :"+p2); mycircle c1=new mycircle(p1,r); mycircle c2=(mycircle)c1.clone();
c1.printarea(); c1.centerat(); c2.printarea(); c2.centerat(); } catch(Exception e) { System.out.println(e); } }
} mycircle.java public class mycircle implements Cloneable { private static int cc=1; private mypoint p=new mypoint(); private int r; private int index; public mycircle(mypoint p,int r) { this.p=p; this.r=r; index=cc++; } public mycircle(mypoint p) { this(p,10); } public Object clone() throws CloneNotSupportedException { mycircle cir1=(mycircle)super.clone(); cir1.p=(mypoint)p.clone(); } public void centerat() { System.out.println("center at :"+p); } public void printarea() { System.out.println("circle "+index+"with area " +Math.PI * r * r); } } mypoint.java public class mypoint implements Cloneable { private int x,y; public mypoint(int x,int y) { this.x=x; this.y=y; }
public mypoint() { this(0,0); } public Object clone() throws CloneNotSupportedException { return super.clone(); } public int getx() { return x; } public int gety() { return y; } public void setx(int x) { this.x=x; } public void sety(int y) { this.y=y; } public String toString() { return "("+x+","+y+")"; } }
高手请帮忙了!!!
[此贴子已经被作者于2005-9-8 17:07:27编辑过]
----------------解决方案--------------------------------------------------------
///////////////////////////////////////////
//
// try it :-)
//
///////////////////////////////////////////
import javax.swing.JOptionPane;
import java.lang.CloneNotSupportedException;
public class Circled
{
private static String heads ="this is a cirle";
public static void main(String argv[])
{
try
{
System.out.println(heads);
String inp;
int r;
inp=JOptionPane.showInputDialog("input radius:");
r=Integer.parseInt(inp);
MyPoint p1=new MyPoint(10,10);
MyPoint p2=(MyPoint)p1.clone();
System.out.println("center at :"+p1);
System.out.println("center at :"+p2);
MyCircle c1=new MyCircle(p1,r);
MyCircle c2=(MyCircle)c1.clone();
c1.printarea();
c1.centerat();
c2.printarea();
c2.centerat();
}
catch(Exception e)
{
System.err.println(e.toString());
}
}
}
//mycircle.java
class MyCircle implements Cloneable
{
private static int cc=0;
private MyPoint p;
private int r;
private int index;
public MyCircle(MyPoint p,int r)
{
this.p=p;
this.r=r;
index = ++cc;
}
public MyCircle(MyPoint p)
{
this(p,10);
}
public Object clone() throws CloneNotSupportedException
{
MyCircle cir1 = (MyCircle)super.clone();
cir1.p=(MyPoint)p.clone();
cir1.index = ++cc;
return cir1;
}
public void centerat()
{
System.out.println("center at :"+p);
}
public void printarea()
{
System.out.println("circle "+index+ " with area " +Math.PI * r * r);
}
}
//mypoint.java
class MyPoint implements Cloneable
{
private int x,y;
public MyPoint(int x,int y)
{
this.x=x;
this.y=y;
}
public MyPoint()
{
this(0,0);
}
public Object clone() throws CloneNotSupportedException
{
return super.clone();
}
public int getx()
{
return x;
}
public int gety()
{
return y;
}
public void setx(int x)
{
this.x=x;
}
public void sety(int y)
{
this.y=y;
}
public String toString()
{
return "("+x+","+y+")";
}
}
----------------解决方案--------------------------------------------------------
用eclipse编的话这种错误都会有提示的 没有try块根本不会让你编译成功的……第二个错误好像是没有返回类型啊?
----------------解决方案--------------------------------------------------------
谢谢了!!!
高手啊!
----------------解决方案--------------------------------------------------------