一个坏掉的番茄
Published on 2017-09-02 11:31 in 暂未分类 with Simon Ma

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());
	}
}

运行结果

posted @ 2019-09-24 18:33  SimonMa  阅读(259)  评论(0)    收藏  举报