【翻译】Abstract Classes Have Constructors

本文探讨了Java中抽象类的构造函数作用及其必要性。解释了即使抽象类不能被实例化,构造函数仍然存在的原因,并通过示例代码展示了这一特性。
1) In Java , we have default constructors provided for classes by the compiler (in case one is not declared).

2) The abstract classes can't be instantiated by using the new operator because they don't provide full implementation of all the methods.

Then does it make sense to have constructors inside abstract classes. Let us find it using a sample abstract class:


public abstract class Test{
public abstract void doSomething();
}

The decompiled code for this class will show the truth about constructors in abstract classes in Java


public abstract class Test
{
public Test()
{
}
public abstract void doSomething();
}


Woha!! that constructor was provided by Java. Why?

The reason is that when a class extends any other class and an instance of the subclass is created then the constructor calls are chained and the super class constructors are invoked. This rule is no exception for abstract class

Moreover, having constructors in a class doesn't necessarily mean that instance of that class will be created using the new operator, but the constructors are intended for writing any initializing code.
总而言之,有构造函数的类不见得就能用new操作符来实例化
### 面向对象分析顺序图的使用方法 #### 构成要素 顺序图由多个组成部分共同构建而成,这些部分包括生命线、激活条、消息和返回消息等。生命线代表参与交互的对象,在时间轴上的投影;激活条则表示该对象正在处理某个操作的时间段;消息用来展示对象之间的调用关系,而返回消息则是响应的结果传递[^1]。 ```mermaid sequenceDiagram participant Alice as 用户 participant Bob as 数据库 Alice->>Bob: 查询数据(id=123) activate Bob Note over Bob: 处理查询请求... Bob-->>Alice: 返回结果集 deactivate Bob ``` 此代码片段定义了一个简单的Mermaid图表语法来绘制顺序图,其中`participant`关键字后面跟的是参与者名称及其别名(可选),通过箭连接符可以创建不同类型的消息流,并且利用`activate/deactivate`命令标记活动区间。 #### 使用场景 当需要描绘系统内部组件间复杂的通信流程时,顺序图就显得尤为重要。它不仅限于软件开发领域内的需求规格说明书编写阶段,同样适用于架构设计文档准备期间对于业务逻辑实现细节的阐述工作。此外,在测试计划制定环节里也能发挥辅助验证预期行为的作用。 #### 实际案例应用 假设存在一个在线购物平台,顾客提交订单之后会触发一系列后台服务间的互动: - 订单管理模块接收到新订单事件; - 向库存管理系统发送商品数量校验指令; - 如果有足够的存货,则通知支付网关完成交易扣款; - 支付成功后再告知仓库部门安排发货事宜。 上述过程可以通过如下形式展现出来: ```mermaid sequenceDiagram participant Customer as 客户端 participant OrderService as 订单服务 participant StockService as 库存服务 participant PaymentGateway as 支付网关 participant Warehouse as 仓储物流 Customer->>+OrderService: 提交订单(orderId=007) OrderService->>+StockService: 检查库存(stockCheckRequest={itemId:"A",quantity:5}) StockService-->>OrderService: 反馈库存情况(hasEnough=true/false) opt 存货充足情况下 OrderService->>+PaymentGateway: 发起付款(payInfo={amount:99.99,customerId:"CUST_88"}) PaymentGateway-->>OrderService: 确认收款(paymentStatus="SUCCESS") OrderService->>+Warehouse: 下达配送任务(deliveryTask={orderId:"ORD_007",address:"..."}) Warehouse-->>OrderService: 接受任务确认(acceptanceAcknowledge="OK") end OrderService-->>Customer: 响应最终状态(finalResponse={"status":"COMPLETED","message":"您的订单已受理并进入下一步处理."}) ``` 这里采用条件分支结构(`opt`)模拟了根据实际业务规则所作出的不同路径选择,使得整个交互链条更加贴近真实世界的应用环境。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值