JAVA编程题:编一个程序包含一个接口 shape(该接口中定义抽象函数calarea()), 一个圆类和一个长方形类
编写一个程序,包含一个接口shape(该接口中定义抽象函数calarea()),一个圆类和一个长方形类,分别实现接口shape(即需实现其中的calarea()函数),定...
编写一个程序,包含一个接口 shape(该接口中定义抽象函数calarea()), 一个圆类和一个长方形类,分别实现接口shape(即需实现其中的calarea() 函数) ,定义一个test类,在其中创建圆对象和长方形对象,分别计算二者的面积。
展开
1个回答
展开全部
//Shape.java
public interface Shape{
public double calarea();
}
//Rectangle.java
public class Rectangle implements Shape{
private double length;
private double high;
public Rectangle(double length,double high){
this.length = length;
this.high = high;
}
public double calarea(){
return length*high;
}
}
//Circle Circle.java
public class Circle implements Shape{
private double radius;
public double calarea(){
return Math.PI*radius*radius;
}
public Circle(double radius){
this.radius = radius;
}
}
//Test.java
public class Test{
public static void main(String []args){
Shape recShape = new Rectangle(2.7,4.8);
Shape cirShape = new Circle(5.0);
System.out.println(recShape.calarea());
System.out.println(cirShape.calarea());
}
}
直接手写的,辛苦吧
public interface Shape{
public double calarea();
}
//Rectangle.java
public class Rectangle implements Shape{
private double length;
private double high;
public Rectangle(double length,double high){
this.length = length;
this.high = high;
}
public double calarea(){
return length*high;
}
}
//Circle Circle.java
public class Circle implements Shape{
private double radius;
public double calarea(){
return Math.PI*radius*radius;
}
public Circle(double radius){
this.radius = radius;
}
}
//Test.java
public class Test{
public static void main(String []args){
Shape recShape = new Rectangle(2.7,4.8);
Shape cirShape = new Circle(5.0);
System.out.println(recShape.calarea());
System.out.println(cirShape.calarea());
}
}
直接手写的,辛苦吧
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询