请问接口和抽象类相同点和不同点是什么?

liangwd 2001-09-03 09:50:04
应该在什么场合下分别使用这两种类?谢谢!
...全文
185 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
verybigbug 2001-09-04
  • 打赏
  • 举报
回复
所以,很多接口就只有一个函数。
mvp2000 2001-09-03
  • 打赏
  • 举报
回复
接口的优点:可以实现多继承
缺点:需要把所有的方法都实现
抽象类的优点:可以先实现一部分方法,不需要每个方法都过载
缺点:只能单继承
woxin 2001-09-03
  • 打赏
  • 举报
回复
This brings up a question: Should you use an interface or an abstract class? An interface gives you the benefits of an abstract class and the benefits of an interface, so if it’s possible to create your base class without any method definitions or member variables you should always prefer interfaces to abstract classes. In fact, if you know something is going to be a base class, your first choice should be to make it an interface, and only if you’re forced to have method definitions or member variables should you change to an abstract class, or if necessary a concrete class.
//from thinking in java
kjxia 2001-09-03
  • 打赏
  • 举报
回复
抽象类中至少有一个方法是没有实现的,不能有类的实例,其他与普通类一样了
接口中所有的方法都是空的,所有数据成员强制默认为static final

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧