抽象类和抽象方法 -- 《JAVA编程思想》23

在了解抽象类抽象方法前,我们先来看一个多态的例子:

public class Shape {

    private String type;

    public Shape(String type) {
        this.type = type;
    }

    public void getArea() {
        System.out.println("get shape area === 0");
    }

    public void getType() {
        System.out.println("shape type is " + type);
    }
    
}
public class Square extends Shape {

    private int length;

    private int width;

    public Square(String type, int length, int width) {
        super(type);
        this.length = length;
        this.width = width;
    }

    @Override
    public void getArea() {
        int area = length * width;
        System.out.println("get square area === " + area);
    }

}

public class Circle extends Shape {

    private int radius;

    public Circle(String type, int radius) {
        super(type);
        this.radius = radius;
    }

    @Override
    public void getArea() {
        double area = radius * radius * 3.14;
        System.out.println("get circle area === " + area);
    }

}
    public static void main(String[] args) {
        Shape shape = new Shape("shape");
        shape.getArea();
        Shape square = new Square("square", 4, 2);
        square.getArea();
        Shape circle = new Circle("circle", 3);
        circle.getArea();
    }
get shape area === 0
get square area === 8
get circle area === 28.26

上述例子中,将几何形状定义为基类 Shape ,将矩形 Square 和 圆形 Circle 作为其导出类,通过对 getArea() 方法传入不同类型的对象,执行不同的方法行为,从而达到多态的目的。

我们注意到,基类 Shape 中的 getArea() 几乎没有任何意义,在实际应用过程中通常需要指定具体的几何类型(导出类)。

所以,我们并不想让其他人直接调用 Shape 类中的 getArea()方法,但仍想保留多态的特性。此时,可以将基类中的 getArea() 方法定义为抽象方法。

定义抽象方法的形式也非常简单:只需在方法名前加上 abstract 关键字即可,抽象方法内不能书写任何具体的代码。但需要注意的是,一旦类中包含一个抽象方法,此类必须强制定义为抽象类,需要在 class 关键字前加上 abstract 关键字,表示这是一个抽象类

public abstract class Shape {

    private String type;

    public Shape(String type) {
        this.type = type;
    }

    public abstract void getArea();

    public void getType() {
        System.out.println("shape type is " + type);
    }

}

被定义为抽象的方法在导出类中强制要求其重写,不能直接被复用。

在这里插入图片描述
此外,抽象类也不允许被实例化。

在这里插入图片描述
抽象类中的非抽象方法,仍可以通过继承直接使用。

    public static void main(String[] args) {
        Shape square = new Square("square", 4, 2);
        square.getArea();
        square.getType();
        Shape circle = new Circle("circle", 3);
        circle.getArea();
        circle.getType();
    }
get square area === 8
shape type is square
get circle area === 28.26
shape type is circle

小结
定义抽象类,可以阻止的基类对象被创建;定义抽象方法,可以强制要求导出类重写方法。

本次分享至此结束,希望本文对你有所帮助,若能点亮下方的点赞按钮,在下感激不尽,谢谢您的【精神支持】。

若有任何疑问,也欢迎与我交流,若存在不足之处,也欢迎各位指正!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BaymaxCS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值