day09-抽象类案例-图形Shape-完善
This commit is contained in:
19
day09/src/com/inmind/abstract05/test/Circle.java
Normal file
19
day09/src/com/inmind/abstract05/test/Circle.java
Normal 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();
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ public class Rectangle extends Shape{
|
||||
|
||||
//强制要求创建矩形的时候,必定要给我一长宽,才能创建矩形对象
|
||||
public Rectangle(int chang ,int kuan){
|
||||
super(chang,kuan,0);
|
||||
super(chang,kuan);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
9
day09/src/com/inmind/abstract05/test/Square.java
Normal file
9
day09/src/com/inmind/abstract05/test/Square.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package com.inmind.abstract05.test;
|
||||
|
||||
public class Square extends Rectangle{
|
||||
|
||||
//强制要求创建正方形的时候,必定要给我一个边长,才能创建正方形对象
|
||||
public Square(int bc){
|
||||
super(bc,bc);
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user