day09-抽象类案例-图形Shape-完善

This commit is contained in:
2026-05-23 14:21:14 +08:00
parent ddca915d58
commit a7a2d8c51c
5 changed files with 52 additions and 1 deletions

View File

@@ -0,0 +1,19 @@
package com.inmind.abstract05.test;
public class Circle extends Shape{
//强制要求创建圆形的时候,必定要给半径,才能创建圆形对象
public Circle(int r){
super(r);
}
@Override
public double getArea() {
return Math.PI*getR()*getR();
}
@Override
public double getZC() {
return 2*Math.PI*getR();
}
}

View File

@@ -5,7 +5,7 @@ public class Rectangle extends Shape{
//强制要求创建矩形的时候,必定要给我一长宽,才能创建矩形对象
public Rectangle(int chang ,int kuan){
super(chang,kuan,0);
super(chang,kuan);
}

View File

@@ -11,8 +11,22 @@ public abstract class Shape {
this.chang = chang;
this.kuan = kuan;
this.r = r;
//初始化操作
}
public Shape(int chang, int kuan) {
/*this.chang = chang;
this.kuan = kuan;*/
this(chang,kuan,0);
}
public Shape(int r) {
// this.r = r;
this(0, 0, r);
}
public int getChang() {
return chang;
}

View File

@@ -0,0 +1,9 @@
package com.inmind.abstract05.test;
public class Square extends Rectangle{
//强制要求创建正方形的时候,必定要给我一个边长,才能创建正方形对象
public Square(int bc){
super(bc,bc);
}
}

View File

@@ -18,5 +18,14 @@ public class Test07 {
Rectangle rectangle = new Rectangle(4, 2);
System.out.println("矩形的面积是:"+rectangle.getArea());
System.out.println("矩形的周长是:"+rectangle.getZC());
//创建一个圆形对象
Circle circle = new Circle(2);
System.out.println("圆形的面积是:"+circle.getArea());
System.out.println("圆形的周长是:"+circle.getZC());
//如何快速定义出一个正方型的类,还不用自己实现周长和面积的方法,但是能输出面积和周长
Square square = new Square(2);
System.out.println("正方形的周长是:"+square.getZC());
System.out.println("正方形的面积是:"+square.getArea());
}
}