java第八次作业

题目:编写一个应用程序,创建一个矩形类,类中具有长、宽两个成员变量和求周长的方法。再创建一个矩形类的子类——正方形类,类中定义求面积方法、重写求周长的方法。在主类中,输入一个正方形边长,创建正方形对象,求正方形的面积和周长。

package com;
import java.util.Scanner;
public class JuXing {
double length;
double wide;
public double getC() {
return (length+wide)*2;
}
static class Zheng extends JuXing{
public double getC() {
return length*4;
}
public double getMj(){
return length*length;
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Zheng z=new Zheng();
System.out.print("请输入边长");
z.length=sc.nextDouble();
System.out.println("面积"+z.getMj());

System.out.println("周长"+z.getC());

运行结果


}
}

原文地址:https://www.cnblogs.com/LJTQ/p/11580364.html