1、创建一个Shape形状类,包括一个getArea求面积方法,一个getC求周长方法 创建3个子类圆、矩形、三角形,分别求他们的面积和周长,并创建test类进行测试

1、创建一个Shape形状类,包括一个getArea求面积方法,一个getC求周长方法
2、创建3个子类Circle圆、Rectangle矩形、Triangle三角形,分别有各自的构造方法,并重新父类的求面积、求周长方法
3、创建一个Test类,对以上的类创建对象进行测试

答案:
下面展示一些 代码

//Shape类的创建
public class Shape {
//求面积的方法
 public double getArea() {
  return 0;
 }
 //求周长的方法
 public double getC() {
  return 0;
 }
}
//子类圆的创建的创建
public class Circle extends Shape{
 protected int r;
 //有形参的构造方法
 public Circle(int r) {
  this.r=r;
 }
 public int getR() {
  return r;
 }
 //方法的重写
 public double getArea() {
  return Math.PI*r*r;
 }
 public double getC() {
  return Math.PI*2*r;
 }
//子类矩形类的继承
public class Rectangle extends Shape {
 private int weight;
 private int height;
 public Rectangle(int weight,int height) {
  this.weight=weight;
  this.height=height;
 }
 public int getweight() {
  return weight;
 }
 public int getheight() {
  return height;
 }
 //方法的重写
 public double getArea() {
  return weight*height;
 }
 public double getC() {
  return 2*weight*height;
 }
//子类三角行类的继承
public class Triangle extends Shape {
 private int a;
 private int b;
 private int c;
 double s;
 public Triangle(int a,int b,int c) {
 //是否能构成三角形
  if(a+b>c&&a+c>b&&b+c>a) {
  this.a=a;
  this.b=b;
  this.c=c;
  }else {
   System.out.println("你输入的三边长无法构成三角形请重新输入");
  }
 }
 public int geta() {
  return a;
 }
 public int getb() {
  return b;
 }
 public int getc() {
  return c;
 }
 //方法的重写
 public double getArea() {
  s=(a+b+c)/2;
  return Math.sqrt((s-a)*(s-b)*(s-c));
 }
 public double getC() {
  return a+b+c;
 }
//进行测试
public class Test1 {
 public static void main(String[]args) {
  Circle circle1=new Circle(4);
  System.out.println("圆的面积是"+circle1.getArea());
  System.out.println("圆的周长是"+circle1.getC());
  Rectangle rectangle1=new Rectangle(3, 4);
  System.out.println("矩形的面积是"+rectangle1.getArea());
  System.out.println("矩形的周长是"+rectangle1.getC());
  Triangle triangle1=new Triangle(3, 4, 5);
  System.out.println("三角形的面积是"+triangle1.getArea());
  System.out.println("三角形的周长是"+triangle1.getC());
 }
  • 13
    点赞
  • 114
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

兢兢业业的码农

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值