设计和使用抽象类(抽象方法)

本文介绍了一个抽象类GeometricObject的设计,用于模拟几何对象的通用属性,并通过Circle和Rectangle类展示了如何继承和实现具体几何形状的面积和周长计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.util.Date;

public abstract class GeometricObject {//创建一个抽象类模拟几何对象的共同特征
    private String color = "white";
    private boolean filled;
    private java.util.Date dateCreated;

    protected GeometricObject() {//抽象类的构造方法定义为protected,因为它只被子类使用
        dateCreated = new java.util.Date();
    }

    protected GeometricObject(String color, boolean filled) {
        //创建一个具体子类的实例时,它的父类的构造方法被调用以初始化父类中定义的数据域
        dateCreated = new java.util.Date();
        this.color = color;
        this.filled = filled;

    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public boolean isFilled() {
        return filled;
    }

    public void setFilled(boolean filled) {
        this.filled = filled;
    }

    public Date getDateCreated() {
        return dateCreated;
    }

    @Override
    public String toString() {
        return "created on " + dateCreated + "\ncolor:" + color + " and filled:" + filled;
    }

    public abstract double getArea();/*因为可以计算所有几何对象的面积和周长,所以最好在GeometricObject类中定义
    getArea()和 getPerimeter()方法。但是这些方法不能在GeometricObiect类中实现。因为他们的实现取决于几何对象的具体类型,
    这样的方法称为抽象方法,在方法头中使用abstract修饰符表示*/

    public abstract double getPerimeter();


}

```java
public class Circle extends GeometricObject {//创建一个圆类继承自几何对象类
    private double radius=1.0;
    public Circle() {
        super();
    }

    public Circle(double radius) {
        super();
        this.radius = radius;
    }

    public Circle(String color, boolean filled, double radius) {
        super(color, filled);
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    @Override
    public double getArea() {//圆类中获取面积方法的具体实现(方法重写)
        return Math.PI * radius * radius;
    }

    @Override
    public double getPerimeter() {//圆类中获取周长方法的具体实现(方法重写)
        return 2 * Math.PI * radius;
    }

}

```java
public class Rectangle extends GeometricObject {//创建一个矩形类继承自几何对象类
    private double width = 1.0;
    private double higth = 1.0;

    public Rectangle() {
        super();
    }

    public Rectangle(double width, double higth) {
        super();
        this.width = width;
        this.higth = higth;
    }

    public Rectangle(String color, boolean filled, double width, double higth) {
        super(color, filled);
        this.width = width;
        this.higth = higth;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public double getHigth() {
        return higth;
    }

    public void setHigth(double higth) {
        this.higth = higth;
    }

    @Override
    public double getArea() {//矩形中获取面积方法的具体实现(方法重写)
        return width * higth;
    }

    @Override
    public double getPerimeter() {//矩形中获取周长方法的具体实现(方法重写)
        return (width + higth) * 2;
    }
}

```java
public class Test {
    public static void main(String[] args) {
        GeometricObject object1 = new Circle(5);//创建一个半径为5的圆,赋值给变量object1
        GeometricObject object2 = new Rectangle(5, 3);/*创建一个边长为3和5的矩形,赋值给变量object2
        这两个变量都是GeometricObject类型的*/
        System.out.println("The two objects have the same area ?" + equalArea(object1, object2));/*
        调用equalArea方法来检验他们的面积是否相同*/
        display(object1);//调用display方法来显示它们,使用的是Circle类中定义的getArea()和getPerimeter()方法
        display(object2);//使用的是Rectangle类中定义的getArea()和getPerimeter()方法
        System.out.println(object1.toString());

    }

    public static boolean equalArea(GeometricObject object1, GeometricObject object2) {
        return object1.getArea() == object2.getArea();/*由于方法重写,object1.getArea()使用的是
        Circle类定义的getArea()方法,object2.getArea()使用的是Rectangle类中定义的getArea()方法*/
    }

    public static void display(GeometricObject object) {
        System.out.println();
        System.out.println("The area is " + object.getArea());
        System.out.println("Thhe perimeter is " + object.getPerimeter());
    }
}

```java
"D:\java 12.0\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2019.2\lib\idea_rt.jar=60547:C:\Program Files\JetBrains\IntelliJ IDEA 2019.2\bin" -Dfile.encoding=UTF-8 -classpath D:\IDEA\untitled6\out\production\untitled6 Test
The two objects have the same area ?false

The area is 78.53981633974483
Thhe perimeter is 31.41592653589793

The area is 15.0
Thhe perimeter is 16.0
created on Sat Apr 11 20:31:06 CST 2020
color:white and filled:false

Process finished with exit code 0



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值