本帖最后由 王德升 于 2012-6-3 23:13 编辑
interface Shape
{
double getArea();
}
class Rec implements Shape
{
private int len,wid;
Rec(int len,int wid)
{
this.len = len;
this.wid = wid;
}
public double getArea()
{
return len * wid;
}
}
class ExceptionTest1
{
public static void main(String[] args)
{
Rec c = new Rec(3,9);
c.getArea();
}
}
毕老师说换成double类型的,可是我试了,为什么编译和运行都可以通过但是就是冒烟结果呢,
请大家帮我看看是怎么回事。
class NoValueException extends RuntimeException
{
NoValueException(String message)
{
super(message);
}
}
interface Shape
{
void getArea();
}
class Rec implements Shape
{
private int len,wid;
Rec(int len,int wid)//throws NoValueException
{
if(len<=0 || wid<=0)
throw new NoValueException("出错了");
this.len = len;
this.wid = wid;
}
public void getArea()
{
System.out.println(len*wid);
}
}
class Crecl implements Shape
{
private int r;
private static double PI = 3.14;
Crecl(int r)
{
this.r = r;
}
public void getArea()
{
System.out.println(r*r*PI);
}
}
class ExceptionTest
{
public static void main(String[] args)
{
//try
//{
Rec r = new Rec(3,9);
r.getArea();
//}
//catch(NoValueException e)
//{
//System.out.println(e.toString());
//}
Crecl c = new Crecl(-9);
c.getArea();
}
}
这段代码中无论我往Crecl里面传正数还是负数结果都是正的,这是为什么呢,?
D:\java0420\day10>javac ExceptionTest.java
D:\java0420\day10>java ExceptionTest
27
254.34
就是这结果,很是郁闷啊。 |
|