用java编写类Circle、Square、和Rectangle,实现上述接口,用于计算圆、正方形、长方形面积
Shape接口定义如下:interfaceShape{doublepi=3.14;doublearea();}用java编写类Circle、Square、和Rectang...
Shape接口定义如下:
interface Shape{
double pi=3.14;
double area();}
用java编写类Circle、Square、和Rectangle,实现上述接口,用于计算圆、正方形、长方形面积 展开
interface Shape{
double pi=3.14;
double area();}
用java编写类Circle、Square、和Rectangle,实现上述接口,用于计算圆、正方形、长方形面积 展开
2个回答
展开全部
public interface Shape
{
double pi=3.14;
double area();
}
public class Circle implements Shape
{
private double radius;
public Circle(double radius)
{
this.radius=radius;
}
public double area()
{
double area;
area=pi*radius*radius;
System.out.println("area="+area);
return area;
}
public static void main(String[] args)
{
Shape ox=new Circle(2.0);
ox.area();
}
}
public class Square implements Shape
{
private double length;
public Square(double length)
{
this.length=length;
}
public double area()
{
double area;
area=length*length;
System.out.println("area="+area);
return area;
}
public static void main(String[] args)
{
Shape ox=new Square(2.0);
ox.area();
}
}
public class Rectangle implements Shape
{
private double length;
private double width;
public Rectangle(double length,double width)
{
this.length=length;
this.width=width;
}
public double area()
{
double area;
area=length*width;
System.out.println("area="+area);
return area;
}
public static void main(String[] args)
{
Shape ox=new Rectangle(2.0,4.0);
ox.area();
}
}我把它写在三个不同的代码里,你要是愿意改那就把它写在一起也可以,用内部类吧。
{
double pi=3.14;
double area();
}
public class Circle implements Shape
{
private double radius;
public Circle(double radius)
{
this.radius=radius;
}
public double area()
{
double area;
area=pi*radius*radius;
System.out.println("area="+area);
return area;
}
public static void main(String[] args)
{
Shape ox=new Circle(2.0);
ox.area();
}
}
public class Square implements Shape
{
private double length;
public Square(double length)
{
this.length=length;
}
public double area()
{
double area;
area=length*length;
System.out.println("area="+area);
return area;
}
public static void main(String[] args)
{
Shape ox=new Square(2.0);
ox.area();
}
}
public class Rectangle implements Shape
{
private double length;
private double width;
public Rectangle(double length,double width)
{
this.length=length;
this.width=width;
}
public double area()
{
double area;
area=length*width;
System.out.println("area="+area);
return area;
}
public static void main(String[] args)
{
Shape ox=new Rectangle(2.0,4.0);
ox.area();
}
}我把它写在三个不同的代码里,你要是愿意改那就把它写在一起也可以,用内部类吧。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询