题目:
通过程序设计几何图形(Shape)、矩形(Rectangle)、圆形(Circle)、正方形(Square)几种类型,能够利用接口和多态性计算几何图形的面积和周长并显示。
package akko;
//图形类
interface Perimeter{
public void Perimeter();//周长
}
interface Area{
public void Area();//面积
}
abstract class Confluence implements Perimeter,Area {
int Length,width,Radius,Perimeter,Area;
public Confluence(int Length,int width,int Radius) {
this.Length = Length;//长
this.width = width;//宽
this.Radius = Radius;//半径
}
}
class Rectangle extends Confluence{
public Rectangle(int Length, int width, int Radius) {
super(Length, width, Radius);
}
public void Perimeter(){
Perimeter = (Length+width)*2;
System.out.println("矩形的周长是:"+Perimeter);
}public void Area(){
Area = Length*width;
System.out.println("矩形的面积是:"+Area);
}
}
class Circle extends Confluence{
public Circle(int Length, int width, int Radius) {
super(Length, width, Radius);
}
public void Perimeter(){
Perimeter = (int) (2*3.14*Radius);
System.out.println("圆形的周长是:"+Perimeter);
}
public void Area(){
Area = (int) (3.14*(Radius*Radius));
System.out.println("圆形的面积是:"+Area);
}
}
//方法的总汇
class Square extends Confluence{
public Square(int Length, int width, int Radius) {
super(Length, width, Radius);
}
public void Perimeter(){
Perimeter = (Length+width)*2;
System.out.println("正方形的周长是:"+Perimeter);
}
public void Area(){
Area = Length*width;
System.out.println("正方形的面积是:"+Area);
}
}
public class Shape {
public static void main(String[] args) {
Rectangle re = new Rectangle(3,7, 0);
re.Perimeter();
re.Area();
Circle ci = new Circle(0, 0, 8);
ci.Perimeter();
ci.Area();
Square sq = new Square(15, 15, 0);
sq.Perimeter();
sq.Area();
}
}