抽象类与接口的区别

    抽象类与接口的区别(英文描述):

    Interface is open to the public, there can not have the private methods and variables.It is used to let others use, but the abstract class can have private  methods or private variables.In addition, the realization of the interface must be realized in the interface definition of all methods, and the realization  of the abstract category have the choice of rewriting need to use the method, the general application, is the top of the interface, and then to achieve an  abstract category Interface, and finally to achieve specific category. Furthermore, the interface can be achieved multiple inheritance, a class can only  inherit a super-category, but can be inherited through multiple interface multiple inheritance, the interface is also available logo (There are no any way,  such as the Remote Interface) and data Sharing (there are all the variables constant) role.

    中文翻译:接口是公开的,里面不能有私有的方法或变量,是用于让别人使用的,而抽象类是可以有私有方法或私有变量的,另外,实现接口的一定要实现接口里定义的所有方法,而实现抽象类可以有选择地重写需要用到的方法,一般的应用里,最顶级的是接口,然后是抽象类实现接口,最后才到具体 类实现。还有,接口可以实现多重继承,而一个类只能继承一个超类,但可以通过继承多个接口实现多重继承,接口还有标识(里面没有任何方法,如Remote接口)和数据共享(里面的变 量全是常量)的作用。

    抽象类和接口的区别在于使用动机。使用抽象类是为了代码的复用,而使用接口的动机是为了实现多态性。

    抽象类和它的子类之间应该是一般和特殊的关系,是一种"is-a"的关系,子类继承父类所有非private的数据成员和成员方法,而接口仅仅是它的子类应该实现的一组规则。如,交通工具定义成抽象类,汽车、飞机、轮船定义成子类,是可以接受的,因为汽车、飞机、轮船都是一种特殊的交通工具。再譬如Icomparable接口,它只是说,实现这个接口的类必须要可以进行比较,这是一条规则。如果Car这个类实现了Icomparable,只是说,我们的Car中有一个方法可以对两个Car的实例进行比较,可能是比哪辆车更贵,也可能比哪辆车更大。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值