java面向对象——抽象类

news/2024/8/21 8:13:59 标签: java, 开发语言, 抽象类

抽象类

final修饰的类,不能有子类。
abstract修饰的类,不能实例化。
不能用abstract修饰私有方法、静态方法、final的方法、final的类。

私有方法不能重写
避免静态方法使用类进行调用
final的方法不能被重写
final修饰的类不能有子类

示例1

航运公司系统,Vehicle类

java">public abstract class Vehicle{
    public abstract double calcFuelEfficiency();
    public abstract double calcTripDistance();
}

public class Truck extends Vehicle{
    public double calcFuelEfficiency();
    public double calcTripDistance();
}

public class RiverBarge extends Vehicle{
    public double calcFuelEfficiency();
    public double calcTripDistance();
}

示例2

在这里插入图片描述

GeometricObject

java">public abstract class GeometricObject {
    String color;

    double weight;

    public GeometricObject(String color, double weight) {
        this.color = color;
        this.weight = weight;
    }

    public String getColor() {
        return color;
    }

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

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

    public abstract double findArea();
}

Circle

java">public class Circle extends GeometricObject{
    private double radius;
    public Circle(double radius, String color, double weight){
        super(color, weight);
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

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

    @Override
    public double findArea(){
        return 3.14 * radius * radius;
    }
}

Rectangle

java">public class Rectangle extends GeometricObject{
    private double width;

    private double height;

    public Rectangle(String color, double weight, double width, double height) {
        super(color, weight);
        this.width = width;
        this.height = height;
    }

    public double getWidth() {
        return width;
    }

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

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    @Override
    public double findArea() {
        return width * height;
    }
}

GeometricTest

java">public class GeometricTest {
    public static void main(String[] args) {
        // 抽象类不能创建实例
        Circle circle = new Circle(2.0, "Blue", 3.0);
        System.out.println(circle.findArea());

        Rectangle rectangle = new Rectangle("red", 3.0, 3.0, 4.0);
        System.out.println(rectangle.findArea());
        
    }
}

模板方法设计模式

java">public class TemplateMethodTest {
    public static void main(String[] args) {
        BankTemplateMethod drawMoney = new DrawMoney();
        drawMoney.process();

        System.out.println();

        BankTemplateMethod btm = new ManageMoney();
        btm.process();
    }
}

// 创建抽象类
abstract class BankTemplateMethod{
    public void takeNumber() {
        System.out.println("取号排队");
    }

    public abstract void transact(); // 办理具体业务

    public void evaluate() {
        System.out.println("反馈评分");
    }

    // 模板方法,把基本操作组合到一起
    public final void process() {
        this.takeNumber();
        this.transact();
        this.evaluate();
    }
}


// 子类
class DrawMoney extends BankTemplateMethod {

    @Override
    public void transact() {
        System.out.println("我要取款");
    }
}

class ManageMoney extends BankTemplateMethod{

    @Override
    public void transact() {
        System.out.println("我要理财");
    }
}

http://www.niftyadmin.cn/n/5555774.html

相关文章

MT6985(天玑9200)芯片性能参数_MTK联发科旗舰5G移动平台处理器

MT6985天玑 9200 旗舰移动平台拥有专业级影像、沉浸式游戏和先进移动显示技术,以及更快捷、覆盖更广的 5G 和 支持 Wi-Fi 7 连接,具有高性能、高能效、低功耗表现。率先采用 Armv9 性能核,全部支持纯 64 位应用,开启高能效架构设计…

MATLAB——字符串处理

文章目录 MATLAB——字符串处理字符串处理函数字符串或字符串数组构造 MATLAB——字符串处理 字符串处理函数 MATLAB中的字符串处理函数如下: 函数名称说明eval(string)作为一个MATLAb命令求字符串的值blanks(n)返回一个具有n个空格的字符串deblank去掉字符串末尾…

护网--2

实验要求: 1、办公区设备可以通过电信链路和移动链路上网(多对多的NAT,并且需要保留一个公网IP不能用来转换) 2、分公司设备可以通过总公司的移动链路和电信链路访问到Dmz区的http服务器 3、多出口环境基于带宽比例进行选路,但是,…

鸿蒙语言基础类库:【@ohos.data.storage (轻量级存储)】

轻量级存储 轻量级存储为应用提供key-value键值型的文件数据处理能力,支持应用对数据进行轻量级存储及查询。数据存储形式为键值对,键的类型为字符串型,值的存储数据类型包括数字型、字符型、布尔型。 说明: 开发前请熟悉鸿蒙开发…

【JavaEE】网络编程——TCP

🤡🤡🤡个人主页🤡🤡🤡 🤡🤡🤡JavaEE专栏🤡🤡🤡 文章目录 前言1.网络编程套接字1.1流式套接字(TCP)1.1.1特点1.1.2编码1.1.2.1ServerSo…

wodpress调用指定分类ID目录下的所有子分类目录ID

要在WordPress中调用指定分类ID目录下的所有子分类目录ID,你可以使用以下代码: function get_child_categories_ids($cate_id) {// 获取指定分类对象$category get_term_by(id, $cate_id, category);// 检查分类是否存在if (is_wp_error($category) ||…

[Python学习篇] Python闭包和装饰器

目录 闭包 什么是闭包 闭包的特性 装饰器 什么是装饰器 装饰器的特性 装饰器常用的应用场景 装饰器代码 无参数的装饰器 装饰器语法糖 带任意参数的装饰器 可组合性装饰器 装饰器工厂 类装饰器 闭包 什么是闭包 Python 闭包(Closure)是指…

数据结构(4.4)——求next数组

next数组的作用:当模式串的第j个字符失配时,从模式串的第next[j]的继续往后匹配 求模式串的next数组(手算) next[1] 任何模式串都一样,第一个字符不匹配时,只能匹配下一个子串,因此,往后,next[1]都无脑写…