永发信息网

Shape接口定义如下:

答案:1  悬赏:70  手机版
解决时间 2021-07-27 16:11
  • 提问者网友:伴风望海
  • 2021-07-27 11:12
Shape接口定义如下:
interface Shape{
double pi=3.14;
double area();}
用java编写类Circle、Square、和Rectangle,实现上述接口,用于计算圆、正方形、长方形面积
最佳答案
  • 五星知识达人网友:不甚了了
  • 2021-07-27 12:39

public interface Shape
{
double pi=3.14;
double area();
}
public class Circle implements Shape
{
private double radius;
public Circle(double radius)
{
\x05 this.radius=radius;
}
public double area()
{
\x05 double area;
\x05 area=pi*radius*radius;
\x05 System.out.println(area=+area);
\x05 return area;
}
public static void main(String[] args)
{
\x05 Shape ox=new Circle(2.0);
\x05 ox.area();
}
}
public class Square implements Shape
{
private double length;
public Square(double length)
{
\x05 this.length=length;
}
public double area()
{
\x05 double area;
\x05 area=length*length;
\x05 System.out.println(area=+area);
\x05 return area;
}
public static void main(String[] args)
{
\x05 Shape ox=new Square(2.0);
\x05 ox.area();
}
}
public class Rectangle implements Shape
{
private double length;
private double width;
public Rectangle(double length,double width)
{
\x05 this.length=length;
\x05 this.width=width;
}
public double area()
{
\x05 double area;
\x05 area=length*width;
\x05 System.out.println(area=+area);
\x05 return area;
}
public static void main(String[] args)
{
\x05 Shape ox=new Rectangle(2.0,4.0);
\x05 ox.area();
}
}我把它写在三个不同的代码里,你要是愿意改那就把它写在一起也可以,用内部类吧.
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯