Java试题
现有接口定义如下interfaceshape2D{doublepi=3.14;doublearea();}请编写类Circle和类Rectangle,实现上述接口,用于计...
现有接口定义如下
interface shape2D {
double pi=3.14;
double area(); }
请编写类 Circle 和类 Rectangle ,实现上述接口,用于计算圆和矩形的面积 展开
interface shape2D {
double pi=3.14;
double area(); }
请编写类 Circle 和类 Rectangle ,实现上述接口,用于计算圆和矩形的面积 展开
2个回答
展开全部
public interface shape2D {
double pi=3.14;
double area();
}
-------------------------
public class Circle implements shape2D{
public double area() {
double r = 5;
double area = pi*r*r;
return area;
}
}
---------------------------
public class Rectangle implements shape2D{
public double area() {
double w = 4;
double h = 6;
double area = w*h;
return area;
}
}
-------------------------------
public class Test {
public static void main(String[] args) {
Circle circle = new Circle();
Rectangle rectangle = new Rectangle();
System.out.println(circle.area());
System.out.println(rectangle.area());
}
}
double pi=3.14;
double area();
}
-------------------------
public class Circle implements shape2D{
public double area() {
double r = 5;
double area = pi*r*r;
return area;
}
}
---------------------------
public class Rectangle implements shape2D{
public double area() {
double w = 4;
double h = 6;
double area = w*h;
return area;
}
}
-------------------------------
public class Test {
public static void main(String[] args) {
Circle circle = new Circle();
Rectangle rectangle = new Rectangle();
System.out.println(circle.area());
System.out.println(rectangle.area());
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询