java利用子类求正方形_Java 作业 重写正方形周长方法

题目

编写一个应用程序,再创建一个矩形类,类中具有长,宽两个成员变量和求周长的方法。

再创建一个矩形类子类, 正方形类,类中定义求面积方法,重写周长方法,在主类中,输入一个正方形的边长,在创建正方形对象

求正方形的面积和周长。

源程序

Rect 类

package com.tomotoes.probleam.eight;

/**

* @author Simon

* @project practice

* @package com.tomotoes.probleam.eight

* @date 2019/9/24 18:25

*/

public class Rect {

double length;

double width;

public double getPerimeter() {

return length + width + width + length;

}

}

Square 类

package com.tomotoes.probleam.eight;

/**

* @author Simon

* @project practice

* @package com.tomotoes.probleam.eight

* @date 2019/9/24 18:26

*/

public class Square extends Rect {

public Square(double length) {

this.length = length;

}

@Override

public double getPerimeter() {

return length * 4;

}

public double getArea() {

return length * length;

}

}

主类

package com.tomotoes.probleam.eight;

import java.util.Scanner;

/**

* @author Simon

* @project practice

* @package com.tomotoes.probleam.eight

* @date 2019/9/24 18:27

*/

public class App {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

//创建正方形对象

Square square = new Square(scanner.nextDouble());

System.out.println(square.getArea());

System.out.println(square.getPerimeter());

}

}

运行结果

e9376b486f469a88bfe7efb99bbd08f8.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值