关于接口问题

xiaopangdun 2004-09-23 12:58:45
有以下程序:

interface Area
{
float calculateArea();
}
class Circle implements Area
{
private float x;
private float y;
private float radius;

Circle(float x ,float y,float radius)
{
this.x=x;
this.y=y;
this. radius=radius;
}
public float calculateArea()
{
return((float)Math. PI*radius*radius);
}
}
class Rectangle
{
private float width ;
private float height;
Rectangle(float width,float height)
{
this.width=width;
this.height=height;
}

public float calculateArea()
{
return(width*height);
}
}
public class InterfaceExample
{
public static void main(String[] args)
{
Circle c=new Circle(3,4,8);
System.out.println("Crcle's area="+c.calculateArea());
Rectangle r=new Rectangle(4,6);
System.out.println("Rectangle's area="+r.calculateArea());
}
为什么在定义class Circle时用了implements Area,而在定义class Rectangle 时没有用implements Area.
...全文
102 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wanglei810522 2004-09-23
  • 打赏
  • 举报
回复
我想楼主的程序一定是别人写好的,这个程序的本意我想是让你了解接口,至于为什么Rectangle应该是疏忽了,但是这样绝对不会报错,只是没有用到接口而已!
javafaq2004 2004-09-23
  • 打赏
  • 举报
回复
如果你觉得要实现,你自己可以实现的。
drugon 2004-09-23
  • 打赏
  • 举报
回复
要明白implements的作用,它主要是在java不能多重继承时采用的方法,就好象C++中的类一样,如果你喜欢,你可以继承任意多个类,如果你不愿意,你也可以一个也不继承,这就是为什么在定义class Circle时用了implements Area,而在定义class Rectangle 时没有用implements Area,要看你是怎么设计,你可以继承,也可以不继承。
yesj 2004-09-23
  • 打赏
  • 举报
回复
问题比较奇怪!
unsalted 2004-09-23
  • 打赏
  • 举报
回复
implements Area就是不实现Area接口,你需要实现的时候才implements.
javafaq2004 2004-09-23
  • 打赏
  • 举报
回复
是你自己不implements Area啊!

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧