day09-继承抽象案例--图形
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package com.inmind.test08;
|
||||
|
||||
public class Circle extends Shape{
|
||||
|
||||
public Circle(double radius){
|
||||
super(radius);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getArea() {
|
||||
return Math.PI*getRadius()*getRadius();
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getZC() {
|
||||
return 2*Math.PI*getRadius();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.inmind.test08;
|
||||
|
||||
public class Rectangle extends Shape{
|
||||
|
||||
//继承中构造方法的传递值的方式,指定属性值
|
||||
public Rectangle(double chang ,double width){
|
||||
super(chang,width);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public double getArea() {
|
||||
return getChang()*getWidth();
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getZC() {
|
||||
return 2*(getChang()+getWidth());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.inmind.test08;
|
||||
|
||||
public abstract class Shape {
|
||||
//子类所需的属性
|
||||
private double width;//宽
|
||||
private double chang;//长
|
||||
private double radius;//半径
|
||||
|
||||
public Shape(){
|
||||
|
||||
}
|
||||
|
||||
public Shape(double radius){
|
||||
this.radius = radius;
|
||||
}
|
||||
|
||||
|
||||
public Shape(double chang ,double width){
|
||||
this.chang = chang;
|
||||
this.width = width;;
|
||||
}
|
||||
|
||||
//get/set方法
|
||||
|
||||
|
||||
public double getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
public void setWidth(double width) {
|
||||
this.width = width;
|
||||
}
|
||||
|
||||
public double getChang() {
|
||||
return chang;
|
||||
}
|
||||
|
||||
public void setChang(double chang) {
|
||||
this.chang = chang;
|
||||
}
|
||||
|
||||
public double getRadius() {
|
||||
return radius;
|
||||
}
|
||||
|
||||
public void setRadius(double radius) {
|
||||
this.radius = radius;
|
||||
}
|
||||
|
||||
//计算面积
|
||||
public abstract double getArea();
|
||||
//计算周长
|
||||
public abstract double getZC();
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.inmind.test08;
|
||||
//正方形是一个特殊的矩形,长宽都是一样的
|
||||
public class Square extends Rectangle{
|
||||
public Square(double bc) {
|
||||
super(bc, bc);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.inmind.test08;
|
||||
/*
|
||||
抽象的意义:
|
||||
1.抽象方法,必定会被重写,对某一类事物的功能添加了约束,必须实现
|
||||
2.普通继承的重写:选择性地沿用父类的功能,选择性扩展父类功能
|
||||
*/
|
||||
|
||||
/*
|
||||
- 定义形状抽象类Shape,矩形Rectangle和圆形Circle继承Shape类。
|
||||
- 圆形只能通过指定半径的方式,创建Circle对象。
|
||||
- 矩形只能通过指定长,宽的方法,创建Rectangle对象。
|
||||
|
||||
并且计算出各自的面积和周长
|
||||
|
||||
1.定义抽象类Shape,封装每个图形所需要的属性(长,宽,高,半径等等)
|
||||
2.定义抽象方法,求面积和周长的方法
|
||||
3.定义非抽象子类Rectangle,圆形Circle,并定义指定构造方法来创建对象
|
||||
4.在子类中,实现各自的求面积和周长的功能实现
|
||||
5.在测试类的主方法中,创建指定矩形和圆,调用方法即可
|
||||
*/
|
||||
|
||||
public class Test08 {
|
||||
public static void main(String[] args) {
|
||||
//创建一个矩形对象
|
||||
Rectangle rectangle = new Rectangle(2, 3);
|
||||
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