java类圆周长_1.求解用java写(如三角形,矩型,圆)的的周长,面积,要求用到继承,多态,抽象类,接口,内部类等。...

满意答案

02ae427d08e371d7e90d5b995e828d6d.png

dfgh0011a

2015.09.29

02ae427d08e371d7e90d5b995e828d6d.png

采纳率:54%    等级:7

已帮助:611人

//抽象的形状类

public abstract class Shape{ }

//接口

public interface IDisplay{

void display(); //显示图形的基本信息

double getArea(); //计算面积

double getGirth(); //计算周长

}

//三角形类

public class Triangle extends Shape implements IDisplay{

protected double a;

protected double b;

protected double c;

public Triangle(double a, double b, double c){

this.a = a; this.b = b; this.c = c;

}

@Override public double getArea() {

double s = (a + b + c) / 2;

return Math.sqrt(s*(s-a)*(s-b)*(s-c));

}

@Override public double getGirth() {

return this.a + this.b + this.c;

}

@Override public void display() {

System.out.println("三角形");

System.out.println("边长:" + a + ", " + b + ", " + c);

}

}

//矩形类

public class Rectangle extends Shape implements IDisplay {

protected double width; protected double height;

public Rectangle(double width, double height){

this.width = width;

this.height = height;

}

@Override public double getArea() {

return this.width * this.height;

}

@Override public double getGirth() {

return 2 * ( this.width + this.height);

}

@Override public void display() {

System.out.println("矩形");

System.out.println("宽:" + this.width + ", 高:" + this.height);

}

}

//圆类

public class Circle extends Shape implements IDisplay {

protected double radius;

public Circle(double radius){

this.radius = radius;

}

@Override public double getArea() {

return Math.PI * this.radius * this.radius;

}

@Override public double getGirth() {

return 2 * Math.PI * this.radius;

}

@Override public void display() {

System.out.println("圆");

System.out.println("半径:" + this.radius);

}

}

00分享举报

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值