实施与扩展:何时使用? 有什么不同?

本文翻译自:Implements vs extends: When to use? What's the difference?

请用一种易于理解的语言或一些文章的链接进行解释。


#1楼

参考:https://stackoom.com/question/jTkh/实施与扩展-何时使用-有什么不同


#2楼

extends is for when you're inheriting from a base class (ie extending its functionality). extends适用于您从基类继承(即扩展其功能)的情况。

implements is for when you're implementing an interface . implements是在您实现接口时使用的

Here is a good place to start: Interfaces and Inheritance . 这是一个不错的起点: 接口和继承


#3楼

extends is for extending a class. extends是用于扩展类。

implements is for implementing an interface implements是用于实现接口

The difference between an interface and a regular class is that in an interface you can not implement any of the declared methods. 接口和常规类之间的区别在于,在接口中不能实现任何已声明的方法。 Only the class that "implements" the interface can implement the methods. 只有“实现”接口的类才能实现方法。 The C++ equivalent of an interface would be an abstract class (not EXACTLY the same but pretty much). 接口的C ++等效项将是一个抽象类(不完全相同,但差不多)。

Also java doesn't support multiple inheritance for classes. 同样,java不支持类的多重继承 This is solved by using multiple interfaces. 这可以通过使用多个接口来解决。

 public interface ExampleInterface {
    public void doAction();
    public String doThis(int number);
 }

 public class sub implements ExampleInterface {
     public void doAction() {
       //specify what must happen
     }

     public String doThis(int number) {
       //specfiy what must happen
     }
 }

now extending a class 现在延伸一堂课

 public class SuperClass {
    public int getNb() {
         //specify what must happen
        return 1;
     }

     public int getNb2() {
         //specify what must happen
        return 2;
     }
 }

 public class SubClass extends SuperClass {
      //you can override the implementation
      @Override
      public int getNb2() {
        return 3;
     }
 }

in this case 在这种情况下

  Subclass s = new SubClass();
  s.getNb(); //returns 1
  s.getNb2(); //returns 3

  SuperClass sup = new SuperClass();
  sup.getNb(); //returns 1
  sup.getNb2(); //returns 2

I suggest you do some more research on dynamic binding, polymorphism and in general inheritance in Object-oriented programming 我建议您对面向对象编程中的动态绑定,多态性和一般继承性进行更多研究


#4楼

Implements is used for Interfaces and extends is used to extend a class. 工具用于接口,扩展用于扩展类。

To make it more clearer in easier terms,an interface is like it sound - an interface - a model, that you need to apply,follow, along with your ideas to it. 为了用更简单的术语使其更清晰,界面就像声音-界面-模型,您需要对其进行应用,遵循以及构想。

Extend is used for classes,here,you are extending something that already exists by adding more functionality to it. Extend用于类,在这里,您可以通过添加更多功能来扩展已经存在的内容。

A few more notes: 一些注意事项:

an interface can extend another interface. 一个接口可以扩展另一个接口。

And when you need to choose between implementing an interface or extending a class for a particular scenario, go for implementing an interface. 而且,当您需要在实现接口还是为特定场景扩展类之间进行选择时,请去实现接口。 Because a class can implement multiple interfaces but extend only one class. 因为一个类可以实现多个接口,但只能扩展一个类。


#5楼

Both keywords are used when creating your own new class in the Java language. 使用Java语言创建自己的新类时,将使用两个关键字。

Difference: implements means you are using the elements of a Java Interface in your class. 区别: implements意味着您在类中使用Java接口的元素。 extends means that you are creating a subclass of the base class you are extending. extends表示您正在创建要扩展的基类的子类。 You can only extend one class in your child class, but you can implement as many interfaces as you would like. 您只能在子类中扩展一个类,但可以实现任意数量的接口。

Refer to oracle documentation page on interface for more details. 有关更多详细信息,请参考界面上的oracle文档页面。

This can help to clarify what an interface is, and the conventions around using them. 这可以帮助阐明接口是什么,以及有关使用接口的约定。


#6楼

A class can only "implement" an interface . 一个class只能“实现”一个interface A class only "extends" a class . 一个类仅“扩展”一个class Likewise, an interface can extend another interface . 同样,一个interface可以扩展另一个interface

A class can only extend one other class . class只能扩展一class A class can implement several interface s. 一个class可以实现多个interface

If instead you are more interested in knowing when to use abstract class es and interface s, refer to this thread: Interface vs Abstract Class (general OO) 相反,如果您更想知道何时使用abstract class es和interface ,请参考此线程: 接口与抽象类(通用OO)

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值