请大家看一下哪里需要简化改正一下

来源:10-3 Java 中的抽象类

qq_逝_7

2018-12-25 18:27

package com;

import java.util.Scanner;

public class test1 {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
   Square square1 = new Circle();
   System.out.println("圆的面积为:"+square1.Area()+",圆的周长为:"+square1.Circumference());
   
   Square square2 = new Rectangle();
   System.out.println("矩形的面积为:"+square2.Area()+",矩形的周长为:"+square2.Circumference());
 }

}

abstract class Square{
 abstract float Area();
 abstract float Circumference();
}
class Circle extends Square{
 public int r;
 public float p = 3.14f;
 
 public Circle() {
  Scanner input = new Scanner(System.in);
  System.out.print("请输入圆的半径:");
  r = input.nextInt();
 }
 @Override
 float Area() {
  // TODO Auto-generated method stub
  return r*r*p;
 }

 @Override
 float Circumference() {
  // TODO Auto-generated method stub
  return p*r*2;
 }
 
}

class Rectangle extends Square{
 public int len,wid;
 public Rectangle() {
  // TODO Auto-generated constructor stub
  
  Scanner input = new Scanner(System.in);
  System.out.print("请输入矩形的长:");
  len = input.nextInt();
  System.out.print("请输入矩形的宽:");
  wid = input.nextInt();
 }
 @Override
 float Area() {
  // TODO Auto-generated method stub
  return len*wid;
 }

 @Override
 float Circumference() {
  // TODO Auto-generated method stub
  return (len+wid)*2;
 }
 
}


写回答 关注

1回答

  • 双子星的平衡
    2018-12-27 22:49:08

    没毛病


Java入门第二季 升级版

课程升级!以终为始告别枯燥,在开发和重构中体会Java面向对象编程的奥妙

530653 学习 · 6091 问题

查看课程

相似问题