Java创建Shape类 您所在的位置:网站首页 圆的面积用xy表示 Java创建Shape类

Java创建Shape类

2023-07-02 13:14| 来源: 网络整理| 查看: 265

要求:

定义基类Shape:

变量:name,静态变量:fatherName = “Shape”

方法: get(),set()方法;构造方法。

输出当前对象详细信息的方法printInfo();

静态方法:getFatherName(),setFatherName()

2.定义圆形类Circle,继承基类Shape:

变量:半径radius

常量:圆周率PI

方法:求周长perimeter(),求面积area();构造方法。

重写基类方法printInfo(),使之输出圆对象信息。

3.定义矩形类Rectangle,继承基类Shape:

变量:长length,宽width

方法:求周长perimeter(),求面积area();构造方法。

当对象是正方形的时候,perimeter(),area()进行方法重载

重写基类方法printInfo(),使之输出矩形对象信息。

4.创建测试类ShapeTester, 在main方法中:

(1)创建Circle类对象,

调用printInfo()输出详细信息;

调用getFatherName()输出信息;

调用setFatherName()方法,更改fatherName值为“newShape”。

(2)创建Rectangle类对象:长方形

调用printInfo()输出详细信息;

调用其父类的printInfo()输出详细信息;

调用getFatherName()输出信息,在结果中分析输出值的变化原因。

(3)创建Rectangle类对象:正方形

 调用printInfo()输出详细信息。

建立的Shape包

package Shape; public abstract class Shape { //基类Shape的定义 public String name;//变量name public static String fatherName = "shape" ;//静态变量fatherName = “Shape” final static double PI =3.14;//终结变量 public static String getFatherName() { return fatherName; } public void printInfo() { System.out.println("\n这是父类的printINfo"); } public void setFatherName(String FatherName) { fatherName = FatherName; } } package Shape; public class Circle extends Shape { private static double radius ; //圆形方法 public Circle(double initRadius) { radius = initRadius; } public static double getRadius() { return radius; } public static void setRadius(double newRadius) { radius = newRadius; } //圆形周长方法 public static double perimeter() { return (2*PI*radius); } //圆形面积方法 public static double area() { return (PI*radius*radius); } //圆形printInfo()方法 public void printInfo() { System.out.printf("圆形周长是"+perimeter()+ "圆形面积是"+"%.2f",area()); } } package Shape; public class Rectangle extends Shape { private static float length; private static float width; public Rectangle() {//默认无参方法 this(0,0); } //长方形方法,参数有2,为长和宽 public Rectangle(float initLength, float initWidth) { length = initLength; width = initWidth; } //正方形方法,参数有1,为边长 public Rectangle(float initLength2) { length = initLength2; } public double getLength() { return length; } public static void setLength(float newLength) { length = newLength; } public double getWidth() { return length; } public static void setWidth(float newWidth) { width = newWidth; } //长方形的周长方法 public static double perimeter() { return (2*length+2*width); } //长方形的面积方法 public static double area() { return (length*width); } //正方形边长方法 public static double perimeter(float initLength2) { return (4* initLength2); } //正方形面积方法 public static double area(float initLength2) { return ( initLength2* initLength2); } //长方形printInfo()方法 public void printInfo() { System.out.printf("长方形的周长是"+perimeter()+ "长方形面积是"+area()); super.printInfo(); } //正方形printInfo()方法 public static void printInfo(float initLength2) { System.out.println("正方形的周长是"+perimeter( initLength2)+ "正方形面积是"+area(initLength2)); } } package Shape; //scanner方法需要的头文件 import java.util.Scanner; public class ShapeTester { @SuppressWarnings({ "resource", "unused", "static-access" }) public static void main(String[] args) { while(true) { //创建键盘录入对象 Scanner in=new Scanner(System.in); System.out.println("欢迎使用Shape系统!\n" + "------\n" + "1.圆形\n" + "2.长方形\n" + "3.正方形\n" + "4.退出\n" + "------\n" + "请选择服务:"); //读取int类型值,进入Switch Case语句; int number=in.nextInt(); switch(number) { case 1: try { //从键盘输入圆的半径 System.out.print("请输入圆形半径:"); double newRadius = in.nextDouble(); //创建Circle对象; Circle c =new Circle (newRadius); //通过调用printInfo()输出详细信息; c.printInfo(); //调用getFatherName()输出信息; System.out.println("\n这是一个"+c.fatherName); //以键盘输入的方式更改fatherName值为“newShape”。 System.out.println("请输入要改的父类名:"); String initname = in.next(); //调用setFatherName()方法,为fatherName赋值; c.setFatherName(initname); System.out.println("这是一个"+c.fatherName); }catch (java.util.InputMismatchException e) { System.out.println("输入错误,请重新输入"); } break; case 2: try { //从键盘输入长方形的长; System.out.print("请输入长方形长:"); float newLength = in.nextFloat(); //从键盘输入长方形的宽; System.out.print("请输入长方形宽:"); float newWidth = in.nextFloat(); //创建Rectangle类对象:长方形; Rectangle rec = new Rectangle(newLength,newWidth); //通过调用printInfo()输出详细信息; rec.printInfo(); }catch (java.util.InputMismatchException e) { System.out.println("输入错误,请重新输入"); } break; case 3: try { //从键盘输入正方形的边长; System.out.print("请输入正方形边长:"); float newLength2 = in.nextFloat(); //创建Rectangle类对象:正方形 Rectangle squ = new Rectangle(newLength2); //调用printInfo()输出详细信息。 Rectangle.printInfo(newLength2); }catch (java.util.InputMismatchException e) { System.out.println("输入错误,请重新输入"); } break; case 4: System.out.print("已退出"); System.exit(0); } } } }

 

 

 

初写java,请各位指正。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有