第八次作业-继承
题目
JuXing.java(矩形类)
/*矩形类中定义了三个成员变量length,width,zc,分别代表长宽和周长,另有一个求举行周长的方法。*/ package com; public class JuXing { public int length; public int width; public int zc; int getZc(){ zc=(length+width)*2; return zc;} }
Zfx.java(正方形类)
/*子类正方形继承父类矩形类,两个方法(其中正方形周长方法为方法重写)求正方形面积和周长*/ package com; public class Zfx extends JuXing{ int area; int getArea(){ return area=length*length; } int getZc(){ zc=(length)*4; return zc;} }
App.java(测试类)
/*创建正方形对象,调用求面积周长方法,输出返回值。*/ package com; import java.util.Scanner; public class App { public static void main(String[] args) {
System.out.println("请输入边长"); Scanner reader=new Scanner(System.in); Zfx zfx=new Zfx(); zfx.length=reader.nextInt(); System.out.println("面积"+zfx.getArea()); System.out.println("周长为"+zfx.getZc()); } }
测试运行结果