java编程接口程序
.利用接口编程:(1)创建一个接口Shape,在接口中定义成员变量perimeter和area用以存放周长和面积,定义抽象方法calPerimeter()和calArea...
.利用接口编程:(1)创建一个接口Shape,在接口中定义成员变量perimeter和area用以存放周长和面积,定义抽象方法calPerimeter()和calArea()来计算周长和面积,定义getPerimeter()和getArea()方法来获取周长和面积的值;(2)创建Shape的子类TriAngle、RectAngle、Circle,为其定义必要的构造函数,并各自实现求周长、面积的方法;(3)创建一个测试类ShapeTestDemo,分别实例化上述三个形状的对象,计算并显示各自的周长和面积。
展开
1个回答
展开全部
public interface Shape {
double perimeter = 0;
double area = 0;
public abstract double calPerimeter();
public abstract double calArea();
public double getPerimeter();
public double getArea();
}
public class Circular implements Shape {
private final double PI = 3.1415926;
private double Radius;
public Circular(double radius) {
this.Radius = radius;
}
@Override
public double calPerimeter() {
// TODO Auto-generated method stub
return Radius * PI * 2;
}
@Override
public double calArea() {
// TODO Auto-generated method stub
return Radius * Radius * PI;
}
public double getRadius() {
return Radius;
}
public void setRadius(double radius) {
Radius = radius;
}
public double getPI() {
return PI;
}
@Override
public double getPerimeter() {
// TODO Auto-generated method stub
return calPerimeter();
}
@Override
public double getArea() {
// TODO Auto-generated method stub
return calArea();
}
}
public class ShapeTestDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Circular cir = new Circular(3);
System.out.println(cir.calArea());
System.out.println(cir.calPerimeter());
System.out.println(cir.getPI());
System.out.println(cir.getArea());
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询