package jicheng; /** * @author GUFG * @project math * @package jicheng * @date 2019/9/24 20:49 */ /*定义一个矩形类,创建a b两个成员变量 创建一个求周长方法Girth*/ public class juxing { int a,b; public int girth(){ return 2*(a+b); } }
/* 创建一个父类Juxing的子类square 创建方法Area,创建方法Girth*/ package jicheng; /** * @author GUFG * @project math * @package jicheng * @date 2019/9/24 20:50 */ public class square extends juxing { public int area(){ return a*a; } public int girth(){ return a*4; } }
/*创建主类,定义Scanner输入一个值表示 边长,声明并创建square的对象square,给边 长a赋值,调用方法Area,Girth并输出*/ package jicheng; import java.util.Scanner; /** * @author GUFG * @project math * @package jicheng * @date 2019/9/24 20:53 */ public class text { public static void main(String[] args){ Scanner r=new Scanner(System.in); System.out.print("请输入正方形的边:"); int z=r.nextInt(); square square=new square(); square.a=z; System.out.println("面积是"+square.area()); System.out.println("周长是"+square.girth()); } }