什么时候要用上Java抽象类_java – 什么时候必须使用接口而不是抽象类?

Java How to Program有关抽象类:

Because they’re used only as superclasses in inheritance hierarchies,

we refer to them as abstract superclasses. These classes cannot be

used to instantiate objects, because abstract classes are incomplete.

Subclasses must declare the “missing pieces” to become “concrete” classes,

from which you can instantiate objects. Otherwise, these subclasses, too,

will be abstract.

回答你的问题“使用interfacs的原因是什么?”:

An abstract class’s purpose is to provide an appropriate superclass

from which other classes can inherit and thus share a common design.

与接口相反:

An interface describes a set of methods that can be called on an

object, but does not provide concrete implementations for all the

methods… Once a class implements an interface, all objects of that class have

an is-a relationship with the interface type, and all objects of the

class are guaranteed to provide the functionality described by the

interface. This is true of all subclasses of that class as well.

所以,为了回答你的问题“我想知道什么时候我应该使用接口”,我想你应该使用接口,当你想要一个完整的实现和使用抽象类,当你想要的部分为你的设计(为了可重用性)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值